2011-06-29 60 views
2

我對一些Core Animation圖層的佈局有些複雜的要求。基本上,我有兩個圖層,CGImage內容需要調整大小(保留它們的縱橫比),但也要在其父圖層中間緊挨着。我在下面說明了這一點。你如何對齊和調整圖像CALayer的大小?

(我的兩個插圖顯示層的界限和層B的圖像內容。如果層本身的尺寸較大沒關係。)

Desired Layout

最接近我來到目前爲止,使用CAConstraintLayoutManager是讓它們正確縮放,但集中在它們的超級層的一半(如下)。

Current, incorrect, layout

我他們得到這一點與下面的代碼:

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的圖層,正確佈置,邊緣沒有空白,然後對其進行縮放。儘管如此,我還是無法想出辦法做到這一點。

回答

0

我仍然接受更好的答案,但是我製作了自己的CALayoutManager子類,它實現了一個自定義的-layoutSublayersOfLayer來將子層的幀恰好放在我想要的位置。我還製作了一個+layoutManager方法,以便我的自定義佈局管理器可以簡單地換成CAConstraintLayoutManager