萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS UIKit 動畫 API 全解析:animateWithDuration、transition、Keyframe 動畫

1. UIView.animateWithDuration — 基礎屬性動畫

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)

2. UIView.transitionWithView — 切換/過渡動畫

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)

3. UIView.animateKeyframesWithDuration — 關鍵幀動畫

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)

選型建議

  • 單屬性簡單過渡 → animateWithDuration
  • 視圖增減/切換 → transitionWithView
  • 多階段、時間線精確控制 → animateKeyframesWithDuration
  • 複雜路徑/層級動畫 → 轉用 Core Animation (CABasicAnimation/CAKeyframeAnimation)
copyright © 萬盛學電腦網 all rights reserved