[UIView beginAnimations:nil context:NULL]; // animate the following:
gearKnob.center = startedAtPoint;
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
这是动画,它将UIImageView(gearKnob)从一个点移动到另一个点。问题在于,当对象移动时,我需要相应地更改背景,因此我需要跟踪对象移动时的每个UIImageView位置。如何跟踪其位置?有什么方法或委托可以做到这一点吗?
最佳答案
如果确实需要,这是一种有效的方法。
诀窍是使用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
}
}