6

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.

Community
  • 1
  • 1
Cosmin
  • 2,840
  • 5
  • 32
  • 50
  • might be able to make use of the CAAnimationDelegate method `animationDidEnd:` – WCByrne May 13 '14 at 14:09
  • I am not interested in the didEnd event. I am interested in a callback that gets called during the animation. – Cosmin May 13 '14 at 14:11
  • Keep in mind that it's the presentationLayer that moves, the CALayer, maintains it's position until the animation is finished so "observing" the CALayers certainly won't work. – MDB983 May 13 '14 at 14:29
  • Maybe some insight into what you are trying to accomplish in tracking the animation. – WCByrne May 14 '14 at 19:00
  • 1
    I need to call a method that performs some actions depending on the position of the animated UIView, so I want to call it each time the view changes its position. – Cosmin May 15 '14 at 08:43
  • this answer looks good: http://stackoverflow.com/questions/18827973/core-animation-progress-callback?lq=1 – Sentry.co Feb 20 '16 at 20:07

1 Answers1

1

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.

Vijay Patidar
  • 111
  • 1
  • 9