我將兩個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是自動分層支持的,不需要這個。
嗨 - 感謝您的回覆!我編輯了上面的代碼。無論何時/何地我插入圖層它似乎沒有區別。 zPosition是否有所作爲,但是如果我設置了較高的值,則由於我設定的視角,圖層會縮小。如果我刪除動畫,圖層層次結構是正確的。 – codecowboy 2010-08-23 18:23:03
別人提到,不應該將CALayers與UIViews混合使用。那是我在做什麼? UIView的圖層屬性在某些方面*特殊*? – codecowboy 2010-08-23 18:25:00
UIView都有一個支持層。它*是一個CALayer,所以說你不應該混用CALayer和UIViews是無稽之談。沒有UIView,你不能使用CALayer。 * layer *屬性是特殊的,因爲它是視圖的根層。它有一些特殊的特徵,但是對於大多數情況,它和任何其他層一樣。 您的CATransform3DMakePerspective方法是什麼樣的?你正在進行翻譯還是隻是一個規模? – 2010-08-23 21:06:00