2010-08-22 83 views
0

我將兩個CAText圖層添加到視圖併爲其中的一個添加動畫。我想爲另一層之上的一層製作動畫,但在動畫完成之前,它無法在圖層層次結構中正確定位。任何人都可以看到我做錯了什麼?動畫工作,它只是運行在'topcharlayer2'後面,直到動畫結束。如何在特定索引處添加動畫圖層

- (CABasicAnimation *)topCharFlap 
{ 
CABasicAnimation *flipAnimation; 


flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; 
flipAnimation.toValue  = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(1.57f, 1, 0, 0)]; 
flipAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0, 1, 0, 0)]; 
flipAnimation.autoreverses = NO; 
flipAnimation.duration  = 0.5f; 
flipAnimation.repeatCount = 10; 


return flipAnimation; 
} 

- (id)initWithFrame:(CGRect)frame { 
if ((self = [super initWithFrame:frame])) { 
    [self setBackgroundColor:[UIColor clearColor]]; //makes this view transparent other than what is drawn. 

    [self initChar]; 
} 

return self; 
} 

static CATransform3D CATransform3DMakePerspective(CGFloat z) 
{ 
CATransform3D t = CATransform3DIdentity; 
t.m34 = - 1./z; 
return t; 
} 



-(void) initChar 
{ 
UIFont *theFont = [UIFont fontWithName:@"AmericanTypewriter" size:FONT_SIZE]; 

self.layer.sublayerTransform = CATransform3DMakePerspective(-1000.0f); 

topHalfCharLayer2 = [CATextLayer layer]; 

topHalfCharLayer2.bounds = CGRectMake(0.0f, 0.0f, CHARACTERS_WIDTH, 100.0f); 
topHalfCharLayer2.string = @"R";  
topHalfCharLayer2.font = theFont.fontName; 
topHalfCharLayer2.fontSize = FONT_SIZE; 
topHalfCharLayer2.backgroundColor = [UIColor blackColor].CGColor; 
topHalfCharLayer2.position = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds)); 

topHalfCharLayer2.wrapped = NO; 


topHalfCharLayer1 = [CATextLayer layer]; 


topHalfCharLayer1.bounds = CGRectMake(0.0f, 0.0f, CHARACTERS_WIDTH, 100.0f); 
topHalfCharLayer1.string = @"T"; 


topHalfCharLayer1.font = theFont.fontName; 
topHalfCharLayer1.fontSize = FONT_SIZE; 

topHalfCharLayer1.backgroundColor = [UIColor redColor].CGColor; 
topHalfCharLayer1.position = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds)); 
topHalfCharLayer1.wrapped = NO; 
//topHalfCharLayer1.zPosition = 100; 

[topHalfCharLayer1 setAnchorPoint:CGPointMake(0.5f,1.0f)]; 

[[self layer] addSublayer:topHalfCharLayer1 ]; 
[[self layer] insertSublayer:topHalfCharLayer2 atIndex:0]; 

[topHalfCharLayer1 addAnimation:[self topCharFlap] forKey:@"anythingILikeApparently"]; 

} 

包含此代碼的視圖由loadView中的視圖控制器加載。 initChar方法在視圖的initWithFrame方法中調用。目標是iOS4。我沒有使用setWantsLayer,因爲我已經讀過iOS中的UIView是自動分層支持的,不需要這個。

回答

1

從石英-dev的蘋果郵件列表:

通常在2D情況下,將addSublayer繪製 前上方的新層。但是,我相信這個實現機制是獨立於zPosition的 ,可能只是使用了諸如 畫家的算法。但是,當你添加zPositions和3D的時候,我並不認爲你可以完全依靠圖層排序。但是我實際上並不清楚 如果Apple沒有在您的圖層上設置 zPositions但具有3D變換矩陣集的情況下保證有任何東西。

所以,似乎我必須明確地設置zPosition時,應用3D轉換到圖層。

1

一對夫婦的想法浮現在腦海中:

  • 嘗試前加上「R」層的圖層層次結構啓動動畫。

  • 而不是索引1處插入的「T」層,使用[[self layer] addSublayer: topHalfCharLayer1];添加它,然後做插件的「R」層[[self layer] insertSublayer:topHalfCharLayer2 atIndex:0];

  • 你試過玩圖層z位置?這決定了圖層的外觀。它並沒有實際移動圖層順序,但會改變它們的顯示方式 - 例如,哪些圖層位於其之前/之後。

  • 我還建議您刪除動畫代碼,直到您獲得圖層視圖順序排序。一旦你這樣做了,動畫應該可以工作。

如果您還有其他問題,請在評論中告知我。

此致敬禮。

+0

嗨 - 感謝您的回覆!我編輯了上面的代碼。無論何時/何地我插入圖層它似乎沒有區別。 zPosition是否有所作爲,但是如果我設置了較高的值,則由於我設定的視角,圖層會縮小。如果我刪除動畫,圖層層次結構是正確的。 – codecowboy 2010-08-23 18:23:03

+0

別人提到,不應該將CALayers與UIViews混合使用。那是我在做什麼? UIView的圖層屬性在某些方面*特殊*? – codecowboy 2010-08-23 18:25:00

+0

UIView都有一個支持層。它*是一個CALayer,所以說你不應該混用CALayer和UIViews是無稽之談。沒有UIView,你不能使用CALayer。 * layer *屬性是特殊的,因爲它是視圖的根層。它有一些特殊的特徵,但是對於大多數情況,它和任何其他層一樣。 您的CATransform3DMakePerspective方法是什麼樣的?你正在進行翻譯還是隻是一個規模? – 2010-08-23 21:06:00

0
/* Insert 'layer' at position 'idx' in the receiver's sublayers array. 
* If 'layer' already has a superlayer, it will be removed before being 
* inserted. */ 

open func insertSublayer(_ layer: CALayer, at idx: UInt32) 
相關問題