iOS 使用六种方式实现动画

这里一位移动画为例,旋转动画同理

1,UIView 动画

    [UIView animateWithDuration:4 animations:^{
        self.button4.frame  = CGRectMake(0, 200, 100, 100);
    }];

2 layer transform 动画

    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //InOut 表示进入和出去时都启动动画
    [UIView setAnimationDuration:0.5f];//动画时间
    self.button1.layer.transform = CATransform3DMakeTranslation(0, 200, 0);
    [UIView commitAnimations]; //启动动画

3 view  transform 动画

   [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //InOut 表示进入和出去时都启动动画
    [UIView setAnimationDuration:0.5f];//动画时间
    self.button2.transform = CGAffineTransformMakeTranslation(0, 200);
    [UIView commitAnimations]; //启动动画

4 基本动画 CABasicAnimation

    CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
//    animation.beginTime = CACurrentMediaTime() +1;
    animation.toValue= @(200);
    animation.duration = 4;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
    [self.button3.layer addAnimation:animation forKey:@"animation"];

5 关键帧动画  CAKeyframeAnimation

    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
//    animation.beginTime = CACurrentMediaTime() +1;
    animation.values = @[@0, @200];
    animation.duration = 4;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
    [self.button6.layer addAnimation:animation forKey:@"animation"];

6 iOS 13之后,可以使用view的 transform3D 实现动画

    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //InOut 表示进入和出去时都启动动画
    [UIView setAnimationDuration:0.5f];//动画时间
    if (@available(iOS 13.0, *)) {
        self.butoon5.transform3D = CATransform3DMakeTranslation(0, 100, 0);
    } else {
        // Fallback on earlier versions
    }
    [UIView commitAnimations]; //启动动画

区别,以上动画中 CAKeyframeAnimation  CABasicAnimation 都是继承自 CAPropertyAnimation,

CAPropertyAnimation 继承自 CAAnimation , CAAnimation 支持设置代理

而 transform 动画不支持设置代理

扫描二维码关注公众号,回复: 12905381 查看本文章

还有UIView的transform 是 CGAffineTransform类型,而layer的的transform是CATransform3D 类型

并且transform实现动画效果,必须和 

    [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];配合使用哦,当然也可以放在[UIView animateWithDuration:duration animations:^{ 里面执行

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/114665294
今日推荐