gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:23:31 27 4
gpt4 key购买 nike

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

这是动画,它将 UIImageView (gearKnob) 从一个点移动到另一个点。问题是当对象移动时,我需要相应地更改背景,所以我需要在移动时跟踪每个 UIImageView 位置。我怎样才能追踪它的位置?有什么方法或委托(delegate)可以做到这一点吗?

最佳答案

如果您确实必须这样做,这里有一种有效的方法。诀窍是使用 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

}

}

关于ios - 如何在 iOS UIView 动画中跟踪动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14622678/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com