Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

Any help would be appreciated.

like image 960
Cosmin Avatar asked May 13 '14 13:05

Cosmin


1 Answers

You can add timer, and add timeInterval where you want any action.

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.

like image 52
Vijay Patidar Avatar answered Oct 12 '22 23:10

Vijay Patidar