Swift 动画效果

  1. 原生UIKit CoreAnimation
let ball = UIView(frame: CGRectMake(50, 50, 100, 100))
ball.backgroundColor = UIColor.purpleColor()
ball.layer.cornerRadius = 50
self.view.addSubview(ball)

//动画执行时间, 延迟时间,动画运动曲线,所有变化结果,结束后执行的闭包
// UIView.animateWithDuration(0.5, delay: 1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
//变化结果
//ball.transform = CGAffineTransformMakeScale(2, 2),
    //动画组合Concat
//  ball.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1.2, 1.2), CGAffineTransformMakeTranslation(150, 140))
//            
//ball.backgroundColor = UIColor.grayColor()
//        }, completion: nil)


//弹性动画usingSpringWithDamping 相当于减速运动中减速度,initialSpringVelocity初速度
UIView.animateWithDuration(2, delay: 1.5, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                        ball.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(1.2, 1.2), CGAffineTransformMakeTranslation(150, 140))
        }, completion: nil)

这里写图片描述


2.JNW
先去 git上 https://github.com/jwilling/JNWSpringAnimation
是oc写的,所以要做一些改动,在桥接文件中倒入JNW后,还要再导入UIKit/UIKit.h
这里写图片描述

  • 放大缩小
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let ball = UIView(frame: CGRectMake(50, 50, 100, 100))
        ball.backgroundColor = UIColor.purpleColor()
        ball.layer.cornerRadius = 50
        self.view.addSubview(ball)

        //创建JNW对象
        let scale1 = JNWSpringAnimation(keyPath: "transform.scale")
        //设置阻尼值(动画衰减)
        scale1.damping = 6
        //设置弹性系数(动画振幅) 
        scale1.stiffness = 100
        //设置质量(动画快慢效果) 
        scale1.mass = 2
        //设置初始值与接收值
        scale1.fromValue = 1
        scale1.toValue = 2
        //将JNW赋给ball
        ball.layer.addAnimation(scale1, forKey: scale1.keyPath)
        //默认还原初始状态,手动修改
        ball.transform = CGAffineTransformMakeScale(2, 2)

    }

  • 旋转
        let rotation = JNWSpringAnimation(keyPath: "transform.rotation")
        rotation.damping = 3
        rotation.stiffness = 100
        rotation.mass = 1
        rotation.fromValue = 0
        rotation.toValue = M_PI_2
        ball.layer.addAnimation(rotation, forKey: rotation.keyPath)
        ball.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

这里写图片描述


  • 水平移动
            var angle:Double = 1
            let translation = JNWSpringAnimation(keyPath: "transform.translation.x")
            translation.damping = 6
            translation.stiffness = 100
            translation.mass = 2
            translation.fromValue = 0
            translation.toValue = 400
            ball.layer.addAnimation(translation, forKey: translation.keyPath)
            ball.transform = CGAffineTransformMakeTranslation(400, 0)

这里写图片描述


JNW组合动画,要几个效果创建几个JNW对象就可以了!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值