@interface ViewController ()
@property (nonatomic, weak) CALayer *layer;
// 用來修改位置的按鈕
@property (nonatomic, weak) UIButton *button;
-
(void)viewDidLoad {
[super viewDidLoad];[self setupLayer];
}
pragma mark - 核心動(dòng)畫直接修改layer
-(void)setupLayer {
// MARK: - 1.添加layer到界面上
// 1.創(chuàng)建
CALayer *layer = [CALayer layer];
// 2.設(shè)置屬性,保證能夠看到
layer.backgroundColor = [UIColor yellowColor].CGColor;
// bounds只決定自己的大小 frame 決定位置和大小
layer.bounds = CGRectMake(0, 0, 150, 150);
layer.position = CGPointMake(200, 300);
// 3.添加
[self.view.layer addSublayer:layer];
// 4.賦值
_layer = layer;
}
//方法
-(void)demoLayer1 {
// MARK: - 1.基本動(dòng)畫
// 1.創(chuàng)建基本動(dòng)畫對(duì)象
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.y"];
// 默認(rèn)是0.25″,可以修改
anim.duration = 3;
// 2.設(shè)置屬性
anim.toValue = @200;
anim.byValue = @300;
// 2.2 結(jié)束時(shí)不要閃回去
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
// 3.添加到layer上
[self.layer addAnimation:anim forKey:nil];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self demoLayer3];
}
pragma mark - 旋轉(zhuǎn)
-(void)demoLayer3 {
創(chuàng)建
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
anim.duration = 2;設(shè)置屬性
anim.toValue = @(M_PI * 2 * 5);添加
[self.layer addAnimation:anim forKey:nil];
}
-(void)demoLayer2 {
創(chuàng)建核心動(dòng)畫
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds.size"];設(shè)置動(dòng)畫屬性
anim.toValue = [NSValue valueWithCGSize:CGSizeMake(500, 500)];
// anim.duration = 0.5;
anim.repeatCount = CGFLOAT_MAX;添加到layer上
[self.layer addAnimation:anim forKey:nil];
}