- 每一个UIView都默认关联着一个CALayer,我们成这个layer为root layer
- 所有的非root layer都存在默认的隐式动画,隐式动画默认为1/4秒。
iOS开发中的动画分为两种:
- 一种为UIView动画,又称隐式动画,动画后frame的数值发生了变化。
- 另一种是CALayer动画,又称显示动画,动画后模型层的数据不会发生变化,图形回到原来的位置。
但是在实际开发中,因为UIView可以响应用户交互,所以UIView动画用的多.
一个小小的demo:
- 点击屏幕改变颜色 XXChangeColorViewController
- 点击屏幕改变颜色运用转场动画实现 XXCustomChangeColorViewController
- 点击屏幕改变颜色,以及跟随触摸点改变位置 XXChangeColorAndFrameViewController
三个重要控制器源代码如下
XXChangeColorViewController
#import "XXChangeColorViewController.h"
#define XXRamdomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
@interface XXChangeColorViewController ()
//显示颜色的layer
@property (nonatomic, strong) CALayer *colorLayer;
//colorView
@property (nonatomic, strong) UIView *colorView;
@end
@implementation XXChangeColorViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"点击屏幕改变颜色";
//用layer实现
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(100, 100, 100, 100);
self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:self.colorLayer];
//用view实现
UIView *colorView = [[UIView alloc] init];
self.colorView = colorView;
colorView.frame = CGRectMake(100, 300, 100, 100);
colorView.backgroundColor = [UIColor redColor];
[self.view addSubview:colorView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.colorLayer.backgroundColor = XXRamdomColor.CGColor;
self.colorView.backgroundColor = XXRamdomColor;
}
@end
XXCustomChangeColorViewController
#import "XXCustomChangeColorViewController.h"
#define XXRamdomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
@interface XXCustomChangeColorViewController ()
//colorLayer
@property (nonatomic, strong) CALayer *colorLayer;
@end
@implementation XXCustomChangeColorViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"自定义改变颜色";
//用layer实现
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(100, 100, 100, 100);
self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:self.colorLayer];
//加入转场动画
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
self.colorLayer.actions = @{@"backgroundColor": transition};
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.colorLayer.backgroundColor = XXRamdomColor.CGColor;
}
@end
XXChangeColorAndFrameViewController
#import "XXChangeColorAndFrameViewController.h"
#define XXRamdomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
@interface XXChangeColorAndFrameViewController ()
//colorLayer
@property (nonatomic, strong) CALayer *colorLayer;
@end
@implementation XXChangeColorAndFrameViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"改变颜色以及跟随手势改变位置";
//用layer实现
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(100, 100, 100, 100);
self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:self.colorLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//获取到当前的触摸点
CGPoint point = [[touches anyObject] locationInView:self.view];
//如果触摸点在colorLayer范围内,则改变颜色,否则跟随手指改变位置
if ([self.colorLayer.presentationLayer hitTest:point]) {
self.colorLayer.backgroundColor = XXRamdomColor.CGColor;
}else {
[CATransaction begin];
[CATransaction setAnimationDuration:4.0];
self.colorLayer.position = point;
[CATransaction commit];
}
}
@end
效果如下图
github源代码地址: