Core Animation之CABasicAnimation
1.toValue与byValue
toValue表示最终的,调整到什么位置,byValue表示调整的幅度,调整了:
//位置
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "position"
let positionX:CGFloat = loginButton!.frame.origin.x+0.5*loginButton!.frame.size.width;
let positionY:CGFloat = loginButton!.frame.origin.y+0.5*loginButton!.frame.size.height+100;
animation.toValue = NSValue(cgPoint: CGPoint(x:positionX,y: positionY))
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.translation.y"
animation.toValue = 100
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)
2.缩放:
//位置
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)
3.旋转:
// transform除了scale缩放属性之外还有rotation旋转属性
//旋转
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "transform.rotation.x"
animation.toValue = Double.pi * 2
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)
4.圆角动画:
//圆角
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "cornerRadius"
animation.toValue = 25
animation.duration = 2.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)
5.边框动画
//边框
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)
6.背景颜色:
//颜色
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)
7.边框颜色
//颜色
loginButton?.layer.borderWidth = 5
let animation:CABasicAnimation = CABasicAnimation()
animation.keyPath = "borderColor"
animation.fromValue = UIColor.green.cgColor
animation.toValue = UIColor.cyan.cgColor
animation.duration = 3.0
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
loginButton?.layer.add(animation, forKey: nil)
8.淡入:
//淡入
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)
9.阴影:
//阴影渐变
loginButton?.layer.shadowColor = UIColor.red.cgColor
loginButton?.layer.shadowOpacity = 0.5
// loginButton?.layer.shadowPath = shadowPath.CGPath
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)
网友评论