CAAnimation类是一个抽象类,因此我们不会直接创建CAAnimation类的对象,而是使用其子类对象。其有3个子类,分别为:
其中,CAPropertyAnimiation属性动画又包含两个子类,分别为:
在iOS9发布后,CABasicAnimation基本动画又添加了一个子类CASpringAnimation弹簧动画。
CAAnimation类遵守了CAMediaTiming协议,在该协议中定义了一些常用的与动画播放相关的属性。常用的包括如下几个。
@property CFTimeInterval beginTime;
@property CFTimeInterval duration;
@property float speed;
@property float repeatCount;
@property CFTimeInterval repeatDuration;
@property(copy) NSString *fillMode;
CA_EXTERN NSString * const kCAFillModeForwards;
CA_EXTERN NSString * const kCAFillModeBackwards;
CA_EXTERN NSString * const kCAFillModeBoth;
CA_EXTERN NSString * const kCAFillModeRemoved;
CAAnimationDelegate是CAAnimation类的代理协议,其中定义了与动画播放相关的一些方法,主要包括:
- (void)animationDidStart:(CAAnimation *)anim;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#CABasicAnimation基本动画
CABasicAnimation类是使用最简单的核心动画类,我们只需要指定keyPath,然后设置起始值(fromValue)和目标值(toValue),然后再把该动画对象添加到一个CALayer类的对象上,即可实现动画的播放。
@property(nullable, strong) id fromValue;
@property(nullable, strong) id toValue;
@property(nullable, strong) id byValue;
示例代码
下方的示例代码中,在控制器类中添加了一个CALayer类型的属性myLayer,并且针对myLayer设置了一个平移的动画。
@interface ViewController ()
@property (nonatomic, strong) CALayer *myLayer;
@end
-(CALayer *)myLayer{
if (_myLayer == nil) {
_myLayer = [CALayer layer];
_myLayer.frame = CGRectMake(140, 100, 100, 100);
_myLayer.backgroundColor = [UIColor yellowColor].CGColor;
_myLayer.borderColor = [UIColor redColor].CGColor;
_myLayer.borderWidth = 4.0;
_myLayer.cornerRadius = 2.0;
}
return _myLayer;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view.layer addSublayer:self.myLayer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1. 实例化CABasicAnimation对象
CABasicAnimation *animation = [CABasicAnimation animation];
//2. 设置动画属性
animation.keyPath = @"transform.translation.x";
animation.toValue = @100;
animation.duration = 2.0;
//3. 添加动画对象到一个CALayer类的对象上,播放动画
[self.myLayer addAnimation:animation forKey:nil];å
}
#CATransition转场动画
通过设置CATransition类中的属性,我们可以定制转场动画的效果,常用的属性主要包括:
@property(copy) NSString *type;
@property(nullable, copy) NSString *subtype;
@property float startProgress;
@property float endProgress;
示例:
在下方的示例代码中,我们实现了当点击屏幕时,切换一个UIImageView控件显示图像的动画过程。
@interface ViewController ()
@property (nonatomic ,strong) UIImageView *transitionView;
@end
-(UIImageView *)transitionView{
if (_transitionView == nil) {
_transitionView = [[UIImageView alloc] initWithFrame:CGRectMake(140, 100, 100, 100)];
_transitionView.image = [UIImage imageNamed:@"99ios"];
}
return _transitionView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.transitionView];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//切换图片显示
self.transitionView.image = [UIImage imageNamed:@"football"];
//创建一个CATransition动画对象
CATransition *animation = [CATransition animation];
animation.type = @"fade";
animation.duration = 2.0;
//添加CATransition对象到transitionView的layer上
[self.transitionView.layer addAnimation:animation forKey:nil];
}