UIView/CALayer动画期间的回调

6
我正在寻找一种方法,在动画期间更新视图的位置时得到通知。我已经查看了StackOverflow上不同的帖子,但没有一个能解决我的问题。
将自己添加为观察者以监视不同属性(如frame、position等)并不能帮助我——只有在设置frame时才会被调用,而不是在position动画时。
此外,我尝试了这篇文章中的解决方案:Core animation progress callback,但它也不起作用。
我希望drawInRect、layoutSubviews或类似的东西会被调用,但它们并没有。
任何帮助都将不胜感激。

可能可以利用CAAnimationDelegate方法animationDidEnd:。 - WCByrne
我对didEnd事件不感兴趣,我感兴趣的是在动画期间调用的回调函数。 - Cosmin
请记住,动画效果是由 presentationLayer 执行的,而 CALayer 会一直保持其位置,直到动画结束。因此,“观察” CALayers 是不起作用的。 - MDB983
也许可以提供一些关于您在跟踪动画方面尝试实现的见解。 - WCByrne
1
我需要调用一个方法,根据动画UIView的位置执行一些操作,因此我希望每次视图改变其位置时都调用它。 - Cosmin
这个答案看起来不错:https://dev59.com/PGMk5IYBdhLWcg3w8SXa?lq=1 - Sentry.co
1个回答

1
你可以添加定时器,并在需要执行任何操作的地方添加时间间隔。
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个点,并且在每个点上都有一个传递动画层,我需要执行一些操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接