IOS swift中的动画的实例详解
UIView的通用动画
let view = UIView(frame: CGRectMake(10.0, 10.0, 100.0, 40.0))
self.view.addSubview(view)
view.backgroundColor = UIColor.lightGrayColor()
// 位置改变
var frame = view.frame
UIView.animateWithDuration(0.6, delay: 2.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
() -> Void in
frame.origin.x = 200.0
view.frame = frame
}) {
(finished:Bool) -> Void in
UIView.animateWithDuration(0.6) {
() -> Void in
frame.origin.x = 10.0
view.frame = frame
}
}
CABasicAnimation核心动画
1、CABasicAnimation类只有三个属性:
fromValue:开始值
toValue:结束值
Duration:动画的时间
repeatCount:重复次数
2、通过animationWithKeyPath键值对的方式设置不同的动画效果
transform.scale
transform.scale.x
transform.scale.y
transform.rotation.z
opacity
margin
zPosition
backgroundColor
cornerRadius
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
let view = UILabel(frame: CGRectMake((self.view.frame.size.width - 200.0) / 2, 10.0, 200.0, 40.0))
self.view.addSubview(view)
view.text = "缩放/淡入淡出"
view.textAlignment = .Center
view.adjustsFontSizeToFitWidth = true
view.backgroundColor = UIColor.lightGrayColor()
//
let layer = view.layer
// 开始动画
// 缩放
let scaleAnimate = CABasicAnimation(keyPath: "transform.scale")
scaleAnimate.fromValue = 1.0
scaleAnimate.toValue = 1.5
scaleAnimate.autoreverses = true
scaleAnimate.repeatCount = MAXFLOAT
scaleAnimate.duration = 1.0
// 淡入淡出
let opaqueAnimate = CABasicAnimation(keyPath: "opacity")
opaqueAnimate.fromValue = 0.1
opaqueAnimate.toValue = 1
opaqueAnimate.autoreverses = true
opaqueAnimate.repeatCount = MAXFLOAT
opaqueAnimate.duration = 1.0
layer.addAnimation(scaleAnimate, forKey: "scaleAnimate")
layer.addAnimation(opaqueAnimate, forKey: "opacityAnimate")
// 组合动画
let view3 = UILabel(frame: CGRectMake(10.0, (currentView.frame.origin.y + currentView.frame.size.height + 10.0), 120.0, 40.0))
self.view.addSubview(view3)
view3.text = "组合动画"
view3.textAlignment = .Center
view3.adjustsFontSizeToFitWidth = true
view3.backgroundColor = UIColor.lightGrayColor()
//
let layer3 = view3.layer
// CAAnimationGroup组合动画效果
let rotate: CABasicAnimation = CABasicAnimation()
rotate.keyPath = "tranform.rotation"
rotate.toValue = M_PI
let scale: CABasicAnimation = CABasicAnimation()
scale.keyPath = "transform.scale"
scale.toValue = 0.0
let move: CABasicAnimation = CABasicAnimation()
move.keyPath = "transform.translation"
move.toValue = NSValue(CGPoint: CGPoint(x: 217, y: 230))
let animationGroup:CAAnimationGroup = CAAnimationGroup()
animationGroup.animations = [rotate, scale, move]
animationGroup.duration = 2.0
animationGroup.fillMode = kCAFillModeForwards
animationGroup.removedOnCompletion = false
animationGroup.repeatCount = MAXFLOAT
//
layer3.addAnimation(animationGroup, forKey: nil)










