【问题标题】:Callback during UIVIew/CALayer animationUIVIew/CALayer 动画期间的回调
【发布时间】:2014-05-13 13:39:19
【问题描述】:

我正在寻找一种在动画期间更新视图位置时获得通知的方法。我在 StackOverflow 上查看了其他不同的帖子,但没有一个回答我的问题。

将自己作为观察者添加到不同的属性(如框架、位置等)并没有帮助 - 我只在设置框架时被调用,而不是在位置动画时被调用。

我也尝试过这篇文章中的解决方案:Core animation progress callback,但效果不佳。

我希望 drawInRect 或 layoutSubviews 或类似的东西会被调用,但它没有。

任何帮助将不胜感激。

【问题讨论】:

  • 或许可以利用CAAnimationDelegate方法animationDidEnd:
  • 我对 didEnd 事件不感兴趣。我对动画期间调用的回调感兴趣。
  • 请记住,移动的是presentationLayer,即CALayer,它会保持其位置直到动画完成,因此“观察”CALayer肯定不会起作用。
  • 也许对您在跟踪动画时要完成的工作有所了解。
  • 我需要调用一个方法来根据动画UIView的位置执行一些动作,所以我想在每次视图改变位置时调用它。

标签: ios animation uiview notifications calayer


【解决方案1】:

您可以添加计时器,并在您想要任何操作的地方添加 timeInterval。

let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.delegate = self
   animation.fromValue = 0
   animation.duration = 3
   shapeLayer.add(animation, forKey: "MyAnimation")

   // save shape layer

   self.shapeLayer = shapeLayer

    timer.invalidate() // just in case this button is tapped multiple times

    // start the timer
    timer = Timer.scheduledTimer(timeInterval: 3/5, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)


@objc func timerAction() {
   print("done")
}

我有 5 个点并在每个点上传递动画层我必须做一些动作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-15
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    相关资源
    最近更新 更多