- 原生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)