UIViewAnimation基礎(chǔ)

設(shè)置UIView的屬性:

  • Position 位置
  • Opacity 透明度
  • Scale 比例
  • Color 顏色

設(shè)置變化:

  • Rotation 旋轉(zhuǎn)
  • Repeat 重復
  • Easing 曲線
  • spring 彈簧

動畫方法

  • 在一定時間內(nèi) View 的屬性發(fā)生改變,動畫結(jié)束后回調(diào)。
    classfunc animateWithDuration(duration:NSTimeInterval, animations: () ->Void, completion: ((Bool) ->Void)?)

  • 在一定時間內(nèi) View 的屬性發(fā)生改變。無回調(diào)。
    classfunc animateWithDuration(duration:NSTimeInterval, animations: () ->Void)

  • 可以設(shè)置動畫執(zhí)行的方式
    classfunc animateWithDuration(duration:NSTimeInterval, delay:NSTimeInterval, options:UIViewAnimationOptions, animations: () ->Void, completion: ((Bool) ->Void)?)
  • 參數(shù)說明:
    • duration : 動畫經(jīng)歷時長
    • delay : 延遲時間,在該延遲時間后才執(zhí)行動畫
    • options : 系統(tǒng)提供了許多動畫執(zhí)行的方式,比如以下幾個
      • Repeat : UIViewAnimationOptions 重復
      • CurveEaseInOut : UIViewAnimationOptions 由慢到快再到慢
      • CurveEaseIn : UIViewAnimationOptions 由慢到快
      • CurveEaseOut : UIViewAnimationOptions 由快到慢
      • CurveLinear : UIViewAnimationOptions 勻速
    • animation : UIView動畫結(jié)束時的狀態(tài) ( 比如 : UIView移動到另一點,變成某一種顏色,放大(縮小)后的比例,變化到某一透明度,視圖旋轉(zhuǎn)到某一角度)
    • completion : 動畫結(jié)束時的回調(diào)(這里可以處理一些事件)

  • 可設(shè)置彈跳效果的動畫
    classfunc animateWithDuration(duration:NSTimeInterval, delay:NSTimeInterval, usingSpringWithDamping dampingRatio:CGFloat, initialSpringVelocity velocity:CGFloat, options:UIViewAnimationOptions, animations: () ->Void, completion: ((Bool) ->Void)?)
* 參數(shù)說明:
  * usingSpringWithDamping  : 阻尼(彈性系數(shù))
  * initialSpringVelocity : 初始速率

小示例如下:

1.Position 位置

Position.gif
UIView.animate(withDuration: 1, animations:{
    self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
    self.greenView.center.y = self.view.bounds.height - self.greenView.center.y   
})

UIView.animate(withDuration: 1, delay: 0.5, options: UIViewAnimationOptions.curveEaseInOut, animations: {
    self.redView.center.x = self.view.bounds.width - self.redView.center.x
    self.redView.center.y = self.redView.center.y - self.view.bounds.height
}, completion: nil)

2.Opacity 透明度

Opacity.gif
UIView.animate(withDuration: 1, animations: {
    self.OpacityView.alpha = 0.1
})

3.Scale 比例

Scale.gif
UIView.animate(withDuration: 1, animations: {
    self.scaleView.transform = CGAffineTransform(scaleX: 2.0, y: 2.0)
})

4.Color 顏色

Color.gif
UIView.animate(withDuration: 1, animations: {
    self.backgroundView.backgroundColor = UIColor.black
    self.textLabel.textColor = UIColor.yellow
})

5.Rotation 旋轉(zhuǎn)

Rotation.gif
UIView.animate(withDuration: 1, animations: {
    self.iamgeView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))    
})

6.Repeat 重復

Repeat.gif
UIView.animate(withDuration: 1, animations: {
    self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
})

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.repeat, animations: {
    self.redView.center.x = self.view.bounds.width - self.redView.center.x
}, completion: nil)

UIView.animate(withDuration: 1, delay: 0, options: [UIViewAnimationOptions.repeat, UIViewAnimationOptions.autoreverse], animations: {
    self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)

7.Easing 曲線

Easing.gif
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
    self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
}, completion: nil)

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: {
    self.redView.center.x = self.view.bounds.width - self.redView.center.x
}, completion: nil)

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
    self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
    self.yellowView.center.x = self.view.bounds.width - self.yellowView.center.x
}, completion: nil)

8.spring 彈簧

Spring.gif
UIView.animate(withDuration: 1, animations: {
    self.blueView.center.x = self.view.bounds.width - self.blueView.center.x
})

UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveLinear, animations: {
    self.redView.center.x = self.view.bounds.width  - self.redView.center.x
}, completion: nil)

UIView.animate(withDuration: 3, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 1, options: UIViewAnimationOptions.curveLinear, animations: {
    self.greenView.center.x = self.view.bounds.width - self.greenView.center.x
}, completion: nil)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容