Core Animation 核心动画使用

@interface ViewController ()

@property (nonatomic, weak) CALayer *layer;

// 用来修改位置的按钮

@property (nonatomic, weak) UIButton *button;

  • (void)viewDidLoad {
    [super viewDidLoad];

    [self setupLayer];

}

pragma mark - 核心动画直接修改layer
-(void)setupLayer {

// MARK: - 1.添加layer到界面上
// 1.创建
CALayer *layer = [CALayer layer];

// 2.设置属性,保证能够看到
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.基本动画
// 1.创建基本动画对象
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.y"];


// 默认是0.25″,可以修改
anim.duration = 3;

// 2.设置属性
anim.toValue = @200;
anim.byValue = @300;

// 2.2 结束时不要闪回去
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;


// 3.添加到layer上
[self.layer addAnimation:anim forKey:nil];

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self demoLayer3];
}

pragma mark - 旋转
-(void)demoLayer3 {

  1. 创建
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    anim.duration = 2;

  2. 设置属性
    anim.toValue = @(M_PI * 2 * 5);

  3. 添加
    [self.layer addAnimation:anim forKey:nil];

}

-(void)demoLayer2 {

  1. 创建核心动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds.size"];

  2. 设置动画属性
    anim.toValue = [NSValue valueWithCGSize:CGSizeMake(500, 500)];
    // anim.duration = 0.5;
    anim.repeatCount = CGFLOAT_MAX;

  3. 添加到layer上
    [self.layer addAnimation:anim forKey:nil];

}

你可能感兴趣的:(Core Animation 核心动画使用)