How to track animation in iOS UIView animation
[UIView beginAnimations:nil context:NULL]; // animate the following:
gearKnob.center = startedAtPoint;
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
This is an animation that moves the UIImageView (gearKnob) from point to point. The problem is that when the object moves, I need to change the background accordingly, so I need to keep track of each position of the UIImageView as it moves. How can I track her position? Is there a way or delegate to do this?
source to share
If you really need to, here's an efficient way to do it. The trick is to use the CADisplayLink timer and read the animated property from layer.presentationLayer.
@interface MyViewController ()
...
@property(nonatomic, strong) CADisplayLink *displayLink;
...
@end
@implementation MyViewController {
- (void)animateGearKnob {
// invalidate any pending timer
[self.displayLink invalidate];
// create a timer that synchronized with the refresh rate of the display
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateDuringAnimation)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// launch the animation
[UIView animateWithDuration:0.3 delay:0 options:0 animations:^{
gearKnob.center = startedAtPoint;
} completion:^(BOOL finished) {
// terminate the timer when the animation is complete
[self.displayLink invalidate];
self.displayLink = nil;
}];
}
- (void)updateDuringAnimation {
// Do something with gearKnob.layer.presentationLayer.center
}
}
source to share
As David wrote, you are probably better off running the second animation in parallel.
If you need to pseudo fire something with a timer, you can try CPAccelerationTimer , which allows you to set the Bézier Time Curve for callbacks.
If you really need to, you can monitor key values on gearKnob.layer.presentationLayer.center
. The layer returned by [CALayer presentationLayer] is "a close approximation of the level that is currently displayed on the screen. While the animation is running, you can get this object and use it to get the current values for these animations."
source to share