class func animateWithDuration(duration: NSTimeInterval,
delay: NSTimeInterval,
usingSpringWithDamping: CGFloat,
initialSpringVelocity: CGFloat,
options: UIViewAnimationOptions,
animations: () -> Void,
completion: ((Bool) -> Void)?)
| 參數 | 含義 |
|---|---|
| duration | 動畫總時長 |
| delay | 延遲啟動時間 |
| usingSpringWithDamping | 阻尼比 (0~1),越小彈性越大 |
| initialSpringVelocity | 初始速度,配合阻尼產生彈射效果 |
| options | .Repeat、.Autoreverse、.CurveEaseInOut 等 |
| animations | 可動畫屬性:frame/bounds/center/alpha/backgroundColor/transform 等 |
| completion | 完成回調 |
// 位移
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: {
self.view1.center.y += 100
}, completion: nil)
// 淡入
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: {
self.view2.alpha = 1
}, completion: nil)
// 位移+淡入
UIView.animateWithDuration(5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .Repeat, animations: {
self.view3.center.y -= 100
self.view3.alpha = 1
}, completion: nil)
class func transitionWithView(view: UIView,
duration: NSTimeInterval,
options: UIViewAnimationOptions,
animations: () -> Void,
completion: ((Bool) -> Void)?)
options 必傳,決定過渡效果:
.TransitionFlipFromLeft/Right/Top/Bottom 翻轉.TransitionCurlUp/Down 頁面翻卷.TransitionCrossDissolve 交叉淡化UIView.transitionWithView(containerView, duration: 0.33,
options: .CurveEaseOut | .TransitionCurlDown, animations: {
self.newView.hidden = false
}, completion: nil)
class func animateKeyframesWithDuration(duration: NSTimeInterval,
delay: NSTimeInterval,
options: UIViewKeyframeAnimationOptions,
animations: () -> Void,
completion: ((Bool) -> Void)?)
在 animations 閉包中調用 addKeyframeWithRelativeStartTime 定義多階段:
UIView.animateKeyframesWithDuration(1.5, delay: 0, options: .Repeat, animations: {
UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 0.25) {
self.plane.center.x += 80; self.plane.center.y -= 10
}
UIView.addKeyframeWithRelativeStartTime(0.1, relativeDuration: 0.4) {
self.plane.transform = CGAffineTransformMakeRotation(-CGFloat(M_PI_4)/2)
}
UIView.addKeyframeWithRelativeStartTime(0.25, relativeDuration: 0.25) {
self.plane.center.x += 100; self.plane.center.y -= 50; self.plane.alpha = 0
}
UIView.addKeyframeWithRelativeStartTime(0.51, relativeDuration: 0.01) {
self.plane.transform = CGAffineTransformIdentity
self.plane.center = CGPoint(x: 0, y: originalCenter.y)
}
UIView.addKeyframeWithRelativeStartTime(0.55, relativeDuration: 0.45) {
self.plane.alpha = 1; self.plane.center = originalCenter
}
}, completion: nil)
animateWithDurationtransitionWithViewanimateKeyframesWithDurationCore Animation (CABasicAnimation/CAKeyframeAnimation)