Swift Animation 内容层动画(一)

Posted by Calvin on 2017-06-24

CAlayer 层动画

先初始化一个按钮

loginButton = UIButton(frame: CGRect(x: 20, y: 230, width: self.view.frame.width-20*2,height: 50))
loginButton!.backgroundColor = UIColor(red: 50/255.0, green: 185/255.0, blue: 170/255.0, alpha: 1.0)
loginButton!.setTitle("登陆", for: UIControlState())
self.view.addSubview(loginButton!)

位置动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "position"
animation.byValue = NSValue(cgPoint:CGPoint(x:-20,y:100))
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

缩放动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.scale.x"
animation.fromValue = 1.0
animation.toValue = 0.8
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

旋转动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.rotation"
animation.toValue = 3.14/2
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

位移动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.translation.y"
animation.toValue = 100
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

圆角动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "cornerRadius"
animation.toValue = 15
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

边框动画

loginButton?.layer.borderColor = UIColor.gray.cgColor
loginButton?.layer.cornerRadius = 10.0
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "borderWidth"
animation.toValue = 10
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

颜色渐变动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "backgroundColor"
animation.fromValue = UIColor.green.cgColor
animation.toValue = UIColor.red.cgColor
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)
loginButton?.layer.borderWidth = 5
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "borderColor"
animation.fromValue = UIColor.green.cgColor
animation.toValue = UIColor.cyan.cgColor
animation.duration = 10.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

淡入淡出动画

let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "opacity"
animation.fromValue = UIColor.green.cgColor
animation.toValue = 1.0
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

阴影渐变动画

loginButton?.layer.shadowColor = UIColor.red.cgColor
loginButton?.layer.shadowOpacity = 0.5
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "shadowOffset"
animation.toValue = NSValue(cgSize: CGSize(width: 10, height: 10))
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)

上文代码:点击查看