Я использовал CABaseAnimation
для создания анимации слоя и установите CABaseAnimation
«s делегата viewController
, вот код:CABaseAnimation Делегат
let flyRight = CABasicAnimation(keyPath: "position.x")
flyRight.delegate = self
flyRight.toValue = view.bounds.size.width/2
flyRight.fromValue = -view.bounds.size.width/2
flyRight.fillMode = kCAFillModeBoth
flyRight.duration = 0.5
flyRight.setValue(heading.layer, forKey: "heading")
heading.layer.addAnimation(flyRight, forKey: nil)
flyRight.beginTime = CACurrentMediaTime() + 0.3
flyRight.setValue(username.layer, forKey: "username")
username.layer.addAnimation(flyRight, forKey: nil)
username.layer.position.x = view.bounds.size.width/2
flyRight.beginTime = CACurrentMediaTime() + 0.4
flyRight.setValue(password.layer, forKey: "password")
password.layer.addAnimation(flyRight, forKey: nil)
password.layer.position.x = view.bounds.size.width/2
код метод делегата animationDidStop
выглядит следующим образом:
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
print(anim)
if let layer = anim.valueForKey("heading") as? CALayer {
anim.setValue(nil, forKey: "heading")
let animation = CABasicAnimation(keyPath: "transform.scale")
animation.fromValue = 1.0
animation.toValue = 1.5
animation.duration = 1.0
layer.addAnimation(animation, forKey: nil)
print("heading layer animation did stop")
}
}
но в методе делегата я обнаружил, что консоль печатает три раза, а анимация заголовка слоя останавливается », вот строка печати на консоли:
<CABasicAnimation: 0x7fbe13096b10>
heading layer animation did stop
<CABasicAnimation: 0x7fbe13020bf0>
heading layer animation did stop
<CABasicAnimation: 0x7fbe13096c80>
heading layer animation did stop
Я очень смущен, почему метод печати выполняется три раза.
Спасибо, очень! Ключевой вопрос: addAnimation: forKey: добавляет копию анимации, – david