animationDidStop没被调用?

时间:2017-05-28 16:47:01

标签: ios swift core-animation calayer

我不确定是什么问题,我确保将委托设置为自己,但它仍未被调用。这是代码:

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //animate form
    let flyRight = CABasicAnimation(keyPath: "position.x")
    flyRight.fromValue = -view.bounds.size.width/2
    flyRight.toValue = view.bounds.size.width/2
    flyRight.duration = 0.5
    heading.layer.add(flyRight, forKey: nil)

    flyRight.isRemovedOnCompletion = false
    flyRight.fillMode = kCAFillModeForwards

    flyRight.delegate = self as? CAAnimationDelegate
    flyRight.setValue("form", forKey: "name")
    flyRight.setValue(username.layer, forKey: "layer")
}

以下是animationDidStop

  func animationDidStop(_ anim: CAAnimation!, finished flag: Bool) {
    let nameValue = anim.value(forKey: "name") as? String
    if let name = nameValue {
      if name == "form" {
        let layer: CALayer = anim.value(forKey: "layer")! as! CALayer
        layer.position.x = view.bounds.width/2
        anim.setValue(nil, forKey: "layer")
      }
    }
  }

1 个答案:

答案 0 :(得分:1)

在您的班级声明中,您缺少符合协议

class MyViewController: UIViewController, CAAnimationDelegate {
// ... 
}