如何在iOS UIView动画中跟踪动画

Pau*_*kis 3 uiimageview uiviewanimation ios

[UIView beginAnimations:nil context:NULL]; // animate the following:
gearKnob.center = startedAtPoint;
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

这是动画,它将UIImageView(gearKnob)从一点移动到另一点.问题是当对象移动时,我需要相应地改变背景,所以我需要在移动时跟踪每个UIImageView位置.我如何追踪其位置?是否有任何方法或代表可以做到这一点?

Dom*_*TTI 8

如果你真的需要,这是一个有效的方法.诀窍是使用CADisplayLink计时器并从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's 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

}

}
Run Code Online (Sandbox Code Playgroud)