隐式动画&显式动画 学习笔记

2018-01-21  本文已影响141人  嗯哎嘶唠咯

隐式动画



- (instancetype)presentationLayer

- (instancetype)modelLayer

显式动画


属性动画

CAAnimation 实现了KVC协议,可以通过 -setValue:forKey:-valueForKey:方法存取属性。但是CAAnimation如同NSDictionary可以随意设置键值,so,在有多重动画需要在代理方法中做区分的时候,会显得格外方便:


...

CABasicAnimation *animation = [CABasicAnimation animation];

[self updateHandsAnimated:NO];

animation.keyPath = @"transform";

animation.toValue = [NSValue valueWithCATransform3D:transform];

animation.duration = 0.5;

animation.delegate = self;

[animation setValue:handView forKey:@"handView"];

[handView.layer addAnimation:animation forKey:nil];

...

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag

{

 //set final position for hand view

 UIView *handView = [anim valueForKey:@"handView"];

 handView.layer.transform = [anim.toValue CATransform3DValue];

}

关键帧动画

CAKeyframeAnimation 因为有个path属性,so~可以配合贝塞尔曲线实现一些复杂的动画。

虚拟属性

CALayer没有显式提供角度或者方向之类的属性,为了旋转图层,我们可以对transform.rotation关键路径应用动画,而不是transform本身。


- (void)viewDidLoad

{

 [super viewDidLoad];

 //add the ship

 CALayer *shipLayer = [CALayer layer];

 shipLayer.frame = CGRectMake(0, 0, 128, 128);

 shipLayer.position = CGPointMake(150, 150);

 shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;

 [self.containerView.layer addSublayer:shipLayer];

 //animate the ship rotation

 CABasicAnimation *animation = [CABasicAnimation animation];

 animation.keyPath = @"transform.rotation";

 animation.duration = 2.0;

 animation.byValue = @(M_PI * 2);

 [shipLayer addAnimation:animation forKey:nil];

}

动画组

CABasicAnimationCAKeyframeAnimation仅仅作用于单独的属性,而CAAnimationGroup可以把这些动画组合在一起

过度

CATransition

CATransition是另一个CAAnimation的子类,和别的子类不同,CATransition有一个typesubtype来标识变换效果。


- (IBAction)switchImage

{

 //set up crossfade transition

 CATransition *transition = [CATransition animation];

 transition.type = kCATransitionFade;

 //apply transition to imageview backing layer

 [self.imageView.layer addAnimation:transition forKey:nil];

 //cycle to next image

 UIImage *currentImage = self.imageView.image;

 NSUInteger index = [self.images indexOfObject:currentImage];

 index = (index + 1) % [self.images count];

 self.imageView.image = self.images[index];

}

ps:做转场动画时,可能会经常使用。

动画过程中取消动画


- (void)removeAnimationForKey:(NSString *)key;

- (void)removeAllAnimations;

小结

basicAnimationkeyAnimation 均作用与单一属性(毕竟都是propertyAnimation的子类),animationGroup 可以多个动画组合。

过渡不同于属性动画,适用场景:视图移除添加和控制器转场。

参考链接

上一篇下一篇

猜你喜欢

热点阅读