002-内容层动画
一、Core Animation核心动画
1.特点
1)Core Animation来自iOS的QuartzCore.framework框架
2)直接作用于CALayer图层上,而非UIView上
3)Core Animation的执行过程在后台,不阻塞主线程
4)可以利用CALayer绝大多数属性制作高级动画效果
2.核心动画类结构

二、CABasicAnimation
1.动画效果
可以让CALayer的属性值从某一个值渐变到另一个值
2.属性&方法
1)指定CALayer的属性名
@property(nullable, copy) NSString *keyPath;
2)初始化方法
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
3)CALayer的属性动画起始值
@property(nullable, strong) id fromValue;
4)CALayer的属性动画终止值
@property(nullable, strong) id toValue;
5)CALayer的属性动画的累加值
@property(nullable, strong) id byValue;
3.代码实例
1)位置动画:position、transform.translation.x、transform.translation.y、transform.translation.z
#pragma mark - 界面 - (void)initUI { self.view.backgroundColor = [UIColor whiteColor]; // btn _btn = [UIButton buttonWithType:UIButtonTypeSystem]; _btn.frame = CGRectMake(20, 100, self.view.bounds.size.width - 20 * 2, 45); [_btn setTitle:@"动画" forState:UIControlStateNormal]; [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_btn]; } #pragma mark - 点击事件 - (void)btnClick:(UIButton *)sender { CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init]; // anchorPoint默认为(0.5, 0.5),那么position默认就是中点 basicAnimation.keyPath = @"position"; basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(_btn.center.x, _btn.center.y + 200)]; basicAnimation.duration = 2.0; basicAnimation.fillMode = kCAFillModeForwards; basicAnimation.removedOnCompletion = NO; [_btn.layer addAnimation:basicAnimation forKey:nil]; // key:动画标识 }
2)缩放动画:transform.scale、transform.scale.x、transform.scale.y
#pragma mark - 界面 - (void)initUI { self.view.backgroundColor = [UIColor whiteColor]; // btn _btn = [UIButton buttonWithType:UIButtonTypeSystem]; _btn.frame = CGRectMake(20, 100, self.view.bounds.size.width - 20 * 2, 45); _btn.backgroundColor = [UIColor redColor]; _btn.tintColor = [UIColor whiteColor]; [_btn setTitle:@"动画" forState:UIControlStateNormal]; [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_btn]; } #pragma mark - 点击事件 - (void)btnClick:(UIButton *)sender { CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init]; basicAnimation.keyPath = @"transform.scale.x"; basicAnimation.toValue = @0.5; basicAnimation.duration = 2.0; basicAnimation.fillMode = kCAFillModeForwards; basicAnimation.removedOnCompletion = NO; [_btn.layer addAnimation:basicAnimation forKey:nil]; // key:动画标识 }
3)旋转动画:transform.rotation
#pragma mark - 界面 - (void)initUI { self.view.backgroundColor = [UIColor whiteColor]; // btn _btn = [UIButton buttonWithType:UIButtonTypeSystem]; _btn.frame = CGRectMake(20, 100, self.view.bounds.size.width - 20 * 2, 45); _btn.backgroundColor = [UIColor redColor]; _btn.tintColor = [UIColor whiteColor]; [_btn setTitle:@"动画" forState:UIControlStateNormal]; [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_btn]; } #pragma mark - 点击事件 - (void)btnClick:(UIButton *)sender { CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init]; basicAnimation.keyPath = @"transform.rotation"; basicAnimation.toValue = @(M_PI / 4.0); basicAnimation.duration = 2.0; basicAnimation.fillMode = kCAFillModeForwards; basicAnimation.removedOnCompletion = NO; [_btn.layer addAnimation:basicAnimation forKey:nil]; // key:动画标识 }
4)淡入淡出动画:opacity
#pragma mark - 界面 - (void)initUI { self.view.backgroundColor = [UIColor whiteColor]; // btn _btn = [UIButton buttonWithType:UIButtonTypeSystem]; _btn.frame = CGRectMake(20, 100, self.view.bounds.size.width - 20 * 2, 45); _btn.backgroundColor = [UIColor redColor]; _btn.tintColor = [UIColor whiteColor]; [_btn setTitle:@"动画" forState:UIControlStateNormal]; [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_btn]; } #pragma mark - 点击事件 - (void)btnClick:(UIButton *)sender { CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init]; basicAnimation.keyPath = @"opacity"; basicAnimation.toValue = @0.2; basicAnimation.duration = 2.0; basicAnimation.fillMode = kCAFillModeForwards; basicAnimation.removedOnCompletion = NO; [_btn.layer addAnimation:basicAnimation forKey:nil]; // key:动画标识 }
三、CAKeyframeAnimation
1.动画效果
可以让CALayer的属性值从某一个值渐变到另一个值
2.属性&方法
1)指定CALayer的属性名
@property(nullable, copy) NSString *keyPath;
2)初始化方法
+ (instancetype)animationWithKeyPath:(nullable NSString *)path;
3)CALayer的属性动画起始值
@property(nullable, strong) id fromValue;
4)CALayer的属性动画终止值
@property(nullable, strong) id toValue;
5)CALayer的属性动画的累加值
@property(nullable, strong) id byValue;
3.代码实例
1)CAKeyframeAnimation实现淡出动画效果

浙公网安备 33010602011771号