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

时间:2013-01-31 09:44:44

标签: ios uiimageview uiviewanimation

[UIView beginAnimations:nil context:NULL]; // animate the following:
gearKnob.center = startedAtPoint;
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];

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

3 个答案:

答案 0 :(得分: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

}

}

答案 1 :(得分:1)

正如大卫写的那样,你最好平行运行第二部动画。

如果您需要使用计时器对某些内容进行伪动画处理,您可能需要尝试CPAccelerationTimer,这样可以为回调设置Bézier时序曲线。

如果你真的必须,你应该能够在gearKnob.layer.presentationLayer.center上进行键值观察。 -[CALayer presentationLayer]返回的图层是“当前正在屏幕上显示的图层的近似值。当动画正在进行时,您可以检索此对象并使用它来获取这些动画的当前值。“

答案 2 :(得分:0)

在这种情况下你不能使用直接动画 您必须使用自己的计时器并移动旋钮 使用:NSTimer timerWithTimeInterval: 并在选择器中移动旋钮并获取位置