Delegate是iOS中一种经常使用的模式,在动画中也是同样,咱们能够经过Delegate实现对动画完成过程当中开始、结束时动做的控制
以下是动画的两个代理方法编程
func animationDidStart(anim: CAAnimation) func animationDidStop(anim: CAAnimation, finished flag: Bool)
使用方法也很简单,只需设置代理便可swift
flyRight.delegate = self
CAAnimation类支持KVC编程,这意味着咱们能够将他们做为Dictionary对待动画
flyRight.setValue(username.layer, forKey: "layer")
经过这行代码,咱们能够让CAAnimation简单的Model带上其做用的Layer
因为咱们设置了Key,咱们就能够在delegate中由此判断传入的是哪个CAAnimation对象代理
if let name = anim.valueForKey("name") as? String { if name == "form" { //form field found } }
返回的是AnyObject?类型,须要自行转换为但愿的类型code
let layer = anim.valueForKey("layer") as? CALayer anim.setValue(nil, forKey: "layer") let pulse = CABasicAnimation(keyPath: "transform.scale") pulse.fromValue = 1.25 pulse.toValue = 1.0 pulse.duration = 0.25 layer?.addAnimation(pulse, forKey: nil)
将layer属性的值设为nil能够orm