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 动画不支持设置代理

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值