2010-11-16 54 views
1

我想用下面的代码动画一个UIView的层的子层:iphone:动画一个CALayer的子层

CALayer *sublayer = [[CALayer alloc] init]; 
[sublayer setFrame:myRect]; // myRect is a portion of the view's frame rectangle 
[self.layer addSublayer:sublayer]; 
在我的drawRect

及更高版本:方法:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
anim.duration = 1.0f; 
anim.fromValue = [NSNumber numberWithFloat:1]; 
anim.toValue = [NSNumber numberWithFloat:.5]; 
anim.delegate = self; 
[sublayer addAnimation:anim forKey:@"animateOpacity"]; 

我不不明白为什么我的子图层不动画,因为代码[self.layer addAnimation:anim forKey:@"animateOpacity"];确实为视图的主图层设置了动画效果。

谢谢。

回答

3

drawRect:方法期间不要添加CAAnimation s。 CoreAnimation在需要重新绘制视图的内容(可能在任何时候)时调用它。您应该在视图添加到其父项后添加动画。

2

您不能为框架属性设置动画。改为使边界或位置动画化。