CAAnimation动画系列示例代码第一篇

CAAnimation、CAPropertyAnimation类不能直接使用。一般使用子类CABasicAnimation、CAKeyframeAnimation、CASpringAnimation、CATransition、CAAnimationGroup以及CATransaction。
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    self.animationView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:self.animationView];
    [UIView setAnimationsEnabled:YES];
 }

CABasicAnimation

    CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    ba.toValue = (__bridge id _Nullable)([UIColor redColor].CGColor);
    ba.byValue = (__bridge id _Nullable)([UIColor purpleColor].CGColor);
    ba.duration = 3.0;
    // forKey:可以自定义
    [self.animationView.layer addAnimation:ba forKey:kCATransition];

CAKeyframeAnimation

    CAKeyframeAnimation *kfa = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
    CAKeyframeAnimation *kfa2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    kfa.values = @[(__bridge id _Nullable)([UIColor redColor].CGColor), (__bridge id _Nullable)([UIColor purpleColor].CGColor)];
    kfa.duration = 3.0;
    
    // calculationMode值为kCAAnimationLinear or kCAAnimationCubic,此值必须为@[@0.0, ...... @1.0] 
    // calculationMode值为kCAAnimationDiscrete,此值必须为@[@0.0, ...... @1.0],该数组 count >= 3
    // calculationMode值为kCAAnimationPaced or kCAAnimationCubicPaced,此值忽略
    kfa.keyTimes = @[@0.3, @0.5];
    
    kfa.calculationMode = kCAAnimationCubicPaced;
    kfa.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseIn];
    // create a CGPath that implements two arcs (a bunce)
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.animationView.center.x, self.animationView.center.y);
    CGPathAddCurveToPoint(path, NULL, 74.0, 500.0, 320.0, 500.0, 320.0, 74.0);
    // CGPathAddCurveToPoint(path, NULL, 320.0, 500.0, 566.0, 500.0, 566.0, 74.0);
    kfa2.path = path;
    kfa2.duration = 5.0;
    kfa2.rotationMode = kCAAnimationRotateAuto;
    kfa2.tensionValues = @[@(-1), @(-0.5), @0.5, @1];
    kfa2.continuityValues = @[@(-1), @(-0.5), @0.5, @1];
    kfa2.biasValues = @[@(-1), @(-0.5), @0.5, @1];
    [self.animationView.layer addAnimation:kfa forKey:@"backfroundColor"];
    [self.animationView.layer addAnimation:kfa2 forKey:@"position"];
    

CASpringAnimation

    CASpringAnimation *sa = [CASpringAnimation animationWithKeyPath:@"position.y"];
    sa.initialVelocity = -1;
    sa.fromValue = @100;
    sa.toValue = @500;
    sa.damping = 0.7;
    sa.stiffness = 120;
    sa.duration = 8.0;
    sa.mass = 5;
    [self.animationView.layer addAnimation:sa forKey:@"position.y"];

CATransition

- (void)transitionsAnimation{
 CATransition *transition = [[CATransition alloc] init];
 transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
 transition.duration = 3;
 transition.startProgress = 0.2;
 transition.endProgress = 0.9;
 transition.type = kCATransitionMoveIn;
 transition.subtype = kCATransitionFromRight;
    
 [self.animationView.layer addAnimation:transition forKey:@"transition"];
 }

CAAnimationGroup

- (void)basicAnimationValueFunction{

    // transform.scale transform.translation 对单一方向设置不需配置valueFunction值
    CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"transform"];
    ba.fromValue = @[@1, @1, @1];
    ba.toValue = @[@2, @5, @2];
//    CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"transform"];
//    ba.fromValue = @[@0, @0, @0];
//    ba.toValue = @[@200, @400, @50];
    ba.duration = 3.0;
    ba.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    ba.removedOnCompletion = NO;
    ba.fillMode = kCAFillModeForwards;
    ba.autoreverses = YES;
    
    CAValueFunction *vf = [CAValueFunction functionWithName:kCAValueFunctionScale];
    ba.valueFunction = vf;
    
    [self.animationView.layer addAnimation:ba forKey:kCATransition];
 }

    [self basicAnimationValueFunction];
    [self transitionsAnimation];
    [self.animationView.layer addAnimation:self.animationGroup forKey:@"animationGroup"];

CATransaction

 [CATransaction begin];
    [CATransaction setDisableActions:NO];
    [CATransaction setAnimationDuration:5.0];
    [CATransaction setCompletionBlock:^{
        NSLog(@"completion");
    }];
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithControlPoints:0.1 :0.9 :0.9 :0.1];
    [CATransaction setAnimationTimingFunction:tf];
//    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
//    [CATransaction setValue:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut] forKey:kCATransactionAnimationTimingFunction];
// UIView 控件不能直接操作 .layer 属性, 而要用 [UIView animate.....
    [UIView animateWithDuration:5.0 animations:^{
        self.animationView.center = CGPointMake(100, 400);
        self.animationView.alpha = 0.5;
        self.animationView.layer.cornerRadius = 40;
    }];
    [CATransaction begin];
    [CATransaction commit];
    [CATransaction commit];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值