示例代码为here。
用隐式属性动画替换显式属性动画后,动画就会中断。
显式动画:
-(void)animate:(id)sender {
...
//Transform Animation
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D: CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D: t];
animation.duration = 1.0;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
[subLayer addAnimation:animation forKey:@"transform"];
//Opacity Animation
animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
animation.duration = 1.0;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
[subLayer addAnimation:animation forKey:@"opacity"];
...
}
-(void)reset:(id)sender {
...
//Transform Animation
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = [NSValue valueWithCATransform3D: t];
animation.toValue = [NSValue valueWithCATransform3D: CATransform3DIdentity];
animation.duration = 1.0;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
[subLayer addAnimation:animation forKey:@"transform"];
//Opacity Animation
animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:1.0];
animation.duration = 1.0;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
[subLayer addAnimation:animation forKey:@"opacity"];
...
}
隐式动画:
-(void)animate:(id)sender {
...
//Transform Animation
[CATransaction setAnimationDuration:1];
subLayer.transform = t;
//Opacity Animation
[CATransaction setAnimationDuration:1];
subLayer.opacity = 0;
...
}
-(void)reset:(id)sender {
...
//Transform Animation
[CATransaction setAnimationDuration:1];
subLayer.transform = CATransform3DIdentity;
//Opacity Animation
[CATransaction setAnimationDuration:1];
subLayer.opacity = 1;
...
}
为什么?
答案 0 :(得分:1)
使用隐式动画时,不需要使用CATransaction。 请注意,uikit会禁用作为UIView
根层的图层的隐式动画答案 1 :(得分:-2)
您应该将CALayer的委托设置为视图控制器在适当时间查看的不同内容(不包括nitWithNibName:bundle:,awakeFromNib,viewDidLoad和viewWillAppear:animated),请查看此处:Does iPhone OS support implicit animation? 。
在我的机器上,通过触摸调用动画效果非常好。