計時器他會等待某個時間間隔,然後觸發,向目標對象發送指定的消息。
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 UIView properties
Animation of View Controller transitions
CoreAnimation
OpenGL - 3D
SpriteKit - 2.5D
Dynamic Animation - 物理
可以改變 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")}
}
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.