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

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

这是动画,它将UIImageView(gearKnob)从一个点移动到另一个点。问题是当对象移动时,我需要相应地更改背景,因此我需要跟踪每个UIImageView在移动时的位置。如何跟踪其位置?是否有任何方法或委托来完成这项任务?


你不应该这样做。为背景添加一个类似的动画,持续时间和时间函数相同。 - David Rönnqvist
3个回答

9
如果你确实需要这样做,以下是一种高效的方法。 技巧是使用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

正如David所写的,你最好同时运行第二个动画。

如果你需要使用计时器来伪造动画效果,你可以尝试使用CPAccelerationTimer,它允许你为回调函数设置贝塞尔定时曲线。

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


0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接