2
我對一些Core Animation圖層的佈局有些複雜的要求。基本上,我有兩個圖層,CGImage內容需要調整大小(保留它們的縱橫比),但也要在其父圖層中間緊挨着。我在下面說明了這一點。你如何對齊和調整圖像CALayer的大小?
(我的兩個插圖顯示層的界限和層B的圖像內容。如果層本身的尺寸較大沒關係。)
最接近我來到目前爲止,使用CAConstraintLayoutManager是讓它們正確縮放,但集中在它們的超級層的一半(如下)。
我他們得到這一點與下面的代碼:
layerA.contentsGravity = kCAGravityResizeAspect;
[layerA setConstraints:[NSArray arrayWithObjects:
[CAConstraint constraintWithAttribute:kCAConstraintMinX relativeTo:@"superlayer" attribute:kCAConstraintMinX],
[CAConstraint constraintWithAttribute:kCAConstraintMaxX relativeTo:@"superlayer" kcaConstraintMidX],
[CAConstraint constraintWithAttribute:kCAConstraintMinY relativeTo:@"superlayer" attribute:kCAConstraintMinY],
[CAConstraint constraintWithAttribute:kCAConstraintMaxY relativeTo:@"superlayer" attribute:kCAConstraintMaxY],
nil]];
layerB.contentsGravity = kCAGravityResizeAspect;
[layerB setConstraints:[NSArray arrayWithObjects:
[CAConstraint constraintWithAttribute:kCAConstraintMinX relativeTo:@"superlayer" attribute:kCAConstraintMidX],
[CAConstraint constraintWithAttribute:kCAConstraintMaxX relativeTo:@"superlayer" kcaConstraintMaxX],
[CAConstraint constraintWithAttribute:kCAConstraintMinY relativeTo:@"superlayer" attribute:kCAConstraintMinY],
[CAConstraint constraintWithAttribute:kCAConstraintMaxY relativeTo:@"superlayer" attribute:kCAConstraintMaxY],
nil]];
你怎麼能調整兩層,並對齊它們?
我想到的一個想法是創建一個包含A和B的圖層,正確佈置,邊緣沒有空白,然後對其進行縮放。儘管如此,我還是無法想出辦法做到這一點。