Swift3.0 动画(缩放、晃动、翻转)

Swift3.0写--2.左右晃动

func shakeAnimation(){
//移除self.mineTopView.layer上的所有动画,可以避免多次重复添加
self.mineTopView.layer.removeAllAnimations()
        let momAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        momAnimation.fromValue = NSNumber(value: -0.1) //左幅度
        momAnimation.toValue = NSNumber(value: 0.1) //右幅度
        momAnimation.duration = 0.1
        momAnimation.repeatCount = HUGE //无限重复
        momAnimation.autoreverses = true //动画结束时执行逆动画
        self.momAnimation.isRemovedOnCompletion = false //切出此界面再回来动画不会停止

        self.momAnimation.delegate = self//CAAnimationDelegate 代理中有动画的开始和结束
        self.mineTopView.layer.add(momAnimation, forKey: "centerLayer")
    }
//旋转动画
    func rorateAnimation(holdView: UIView){

        let momAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        momAnimation.fromValue = NSNumber(value: 0) //左幅度
        momAnimation.toValue = NSNumber(value: M_PI*2) //右幅度
        momAnimation.duration = 1
        momAnimation.repeatCount = HUGE //无限重复
        holdView.layer.add(momAnimation, forKey: "centerLayer")
    }

1.缩放

- (void)Animation
{
    // 设定为缩放
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    // 动画选项设定
    animation.duration = 2.0; // 动画持续时间
    animation.repeatCount = HUGE_VALF; // 重复次数(无限)
    animation.autoreverses = YES; // 动画结束时执行逆动画

    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:0.9]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.1]; // 结束时的倍率

    // 添加动画
    [self.moveView.layer addAnimation:animation forKey:@"scale-layer"];
}

2.左右晃动

- (void)animation
{
 //z轴
 CABasicAnimation *momAnimation = [CABasicAnimation     animationWithKeyPath:@"transform.rotation.z"];
    momAnimation.fromValue = [NSNumber numberWithFloat:-0.1];//左幅度
    momAnimation.toValue = [NSNumber numberWithFloat:0.1];//右幅度
    momAnimation.duration = 1.5;
    momAnimation.repeatCount = CGFLOAT_MAX;//无限重复
    momAnimation.autoreverses = YES;//动画结束时执行逆动画
    momAnimation.delegate = self;
    [self.moveView.layer addAnimation:momAnimation forKey:@"centerLayer"];
    }

3.翻转动画

- (void)transitionAnimation
{
    CGContextRef context=UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//可选
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.moveView cache:YES];//UIViewAnimationTransitionCurlUp五种翻转效果可选
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

4.循环动画

[UIView animateWithDuration:timer animations:^{
        //执行
    } completion:^(BOOL finished) {
       //结束后再执行(就是不知无限循环有多大危害)
    }];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值