iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 7
0
自我挑戰組

白鬍子老頭30天 Swift Ios系列 第 27

Day(7+20) NSTimer Animation

  • 分享至 

  • xImage
  •  

NSTimer

計時器他會等待某個時間間隔,然後觸發,向目標對象發送指定的消息。

class func scheduledTimer(
timeInterval ti: TimeInterval, 
target aTarget: Any, 
selector aSelector: Selector, 
userInfo: Any?, 
repeats yesOrNo: Bool) -> Timer

可以這樣寫

let timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) 
{ (timer) in
      print("2sec")
}

2秒後就會印出"2sec"

或是

  let timer = Timer.scheduledTimer(
            timeInterval: 2.0,
            target: self,
            selector: #selector(twoSec),
            userInfo: nil,
            repeats: false
        )
  func twoSec() {
        print("2ec")
  }

停止計時器

timer.invalidate()

Animation

animation的種類

Animation UIView properties
Animation of View Controller transitions
CoreAnimation
OpenGL - 3D
SpriteKit - 2.5D
Dynamic Animation - 物理

Animation UIView properties

可以改變 UIView的
frame
transform
alpha

當animation開始 你設定的值會立刻改變 不會像動畫慢慢補間這樣

class func animate(withDuration duration: TimeInterval, 
             delay: TimeInterval, 
           options: UIViewAnimationOptions = [], 
        animations: @escaping () -> Void, 
        completion: ((Bool) -> Void)? = nil)

animation block 在裡面改變frame transform 之類

example:

     UIView.animate(
        withDuration: 3.0,
        delay: 0,
        options: [UIViewAnimationOptions.curveLinear],
        animations: {
        self.faceView.alpha = 0
        })
        { if $0 {
        print("done")}
        }

Dynamic Animation

Create UIDynqmicAnimator

 var dam = UIDynamicAnimator(referenceView: UIView)

Create and add UIDynamicBehavior

let gravity = UIGravityBehavior()
dam.addBehavior(gravity)

Add UIDynamicItems to behavior

let item :UIDynamicItem = faceView
gravity.addItem(item)

UIDynamaicItem 其實是個Protocol

var bounds: CGRect
//Required. Called when a dynamic animator needs the bounds of the dynamic //item.
var center: CGPoint
//Required. The center point of the dynamic item.
var transform: CGAffineTransform
//Required. The rotation of the dynamic item.

上一篇
Day(7+19) AutoLayout-2
下一篇
Day(7+21) Dynamic Animation Demo
系列文
白鬍子老頭30天 Swift Ios30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言