1 #pragma mark - CABasicAnimation动画 2 - (IBAction)basicAnimation:(UIButton *)sender { 3 4 // 1.创建动画对象 5 CABasicAnimation *basicAnimation = [CABasicAnimation animation]; 6 7 // 2.告诉layer层需要执行什么样的动画【后面设置的内容为CALayer的相关属性】 8 basicAnimation.keyPath = @"position"; 9 10 // 3.告诉layer从哪里来,要到哪里去 11 basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)]; 12 basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)]; 13 14 // 如果要实现移动后不回到原来的位置 15 basicAnimation.removedOnCompletion = NO; 16 basicAnimation.fillMode = kCAFillModeForwards; // 保存动画状态的内容 17 18 // 4.设置动画持续的时长 19 basicAnimation.duration = 6.0f; 20 21 // 5.将要执行的动画添加到CALayer上 22 [self.imgView.layer addAnimation:basicAnimation forKey:@"basic"]; 23 24 25 /***************** 翻转效果 *********************/ 26 CABasicAnimation *basicAnimation1 = [CABasicAnimation animation]; 27 basicAnimation1.keyPath = @"transform"; 28 29 // 设置的翻转到的地方 30 basicAnimation1.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0, 0, 1)]; 31 32 basicAnimation1.duration = 4.0f; 33 [self.imgView.layer addAnimation:basicAnimation1 forKey:@"aaa"]; 34 35 36 // 根据key移除动画 37 [self.imgView.layer removeAnimationForKey:@"basic"]; 38 }
1 #pragma mark - CAKeyFrameAnimation动画 2 - (IBAction)CAKeyFrameAnimation:(UIButton *)sender { 3 4 // 1.创建对象 5 CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation]; 6 7 // 2.设置动画轨迹 8 keyFrameAnimation.keyPath = @"transform.rotation"; 9 10 // 3.设置旋转角度(弧度的计算公式:度数 / 180 * M_PI) 11 keyFrameAnimation.values = @[@(-4 / 180.0 * M_PI), @(4 / 180.0 * M_PI), @(8 / 180.0 * M_PI)]; 12 13 // 4.设置时长 14 keyFrameAnimation.duration = 3.0f; 15 16 // 5.添加动画到layer层 17 [self.imgView.layer addAnimation:keyFrameAnimation forKey:@"ccc"]; 18 19 }
1 #pragma mark - CAAnimationGroup动画 2 - (IBAction)CAAnimationGroup:(UIButton *)sender { 3 4 // 平移动画 5 CABasicAnimation *basic1 = [CABasicAnimation animation]; 6 basic1.keyPath = @"transform.translation.y"; 7 basic1.toValue = @(400); 8 9 // 翻转动画 10 CABasicAnimation *basic2 = [CABasicAnimation animation]; 11 basic2.keyPath = @"transform.scale"; 12 basic2.toValue = @(0.5); 13 14 // 旋转动画 15 CABasicAnimation *basic3 = [CABasicAnimation animation]; 16 basic3.keyPath = @"transform.rotation"; 17 basic3.toValue = @(M_PI); 18 19 20 // 创建管理各个动画的动画组 21 CAAnimationGroup *group = [CAAnimationGroup animation]; 22 group.animations = @[basic1, basic2, basic3]; 23 group.duration = 5.0f; 24 [self.imgView.layer addAnimation:group forKey:@"ddd"]; 25 }
1 #pragma mark - CASpringAnimation动画 2 - (IBAction)CASpringAnimation:(UIButton *)sender { 3 4 CASpringAnimation *springAnimation = [CASpringAnimation animation]; 5 springAnimation.keyPath = @"transform.scale"; 6 springAnimation.fromValue = @1; 7 springAnimation.toValue = @0.25; 8 springAnimation.duration = 3.0f; 9 [self.imgView.layer addAnimation:springAnimation forKey:@"eee"]; 10 }