简体   繁体   English

UIVIew/CALayer 动画期间的回调

[英]Callback during UIVIew/CALayer animation

I am looking for a way to get notified when the position of a view gets updated during the animation.我正在寻找一种在动画期间视图位置更新时获得通知的方法。 I have checked different other posts on StackOverflow but none have the answer to my problem.我在 StackOverflow 上检查过其他不同的帖子,但没有一个能解决我的问题。

Adding myself as an observer to different properties such as frame, position, etc doesn't help - I only get called when I set the frame but not when the position animates.将自己作为观察者添加到不同的属性(例如框架、位置等)并没有帮助 - 我只在设置框架时被调用,而在位置动画时不会被调用。

Also I have tried the solution from this post: Core animation progress callback but it doesn't work as well.我也尝试了这篇文章中的解决方案: 核心动画进度回调,但效果不佳。

I was hoping that drawInRect, or layoutSubviews, or something similar would get called, but it doesn't.我希望 drawInRect 或 layoutSubviews 或类似的东西会被调用,但事实并非如此。

Any help would be appreciated.任何帮助,将不胜感激。

You can add timer, and add timeInterval where you want any action.您可以添加计时器,并在您想要任何操作的地方添加 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")
}

I have 5 dot and passing animation layer on each dot I have to do some action.我有 5 个点并在每个点上传递动画层我必须做一些动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM