gpt4 book ai didi

iphone - 如何暂停和恢复UIView动画?

转载 作者:行者123 更新时间:2023-12-03 19:04:18 27 4
gpt4 key购买 nike

我有一个带有多个 UILabels 的 UIView,它们从上到下进行动画处理,反之亦然。可以说是一种 Autoque :) 我使用 2 个函数:

-(void)goUp 
-(void)goDown

这些函数将 UIView 动画启动到所需的位置。它们都定义了一个在最后调用另一个函数的 AnimationDidStopSelector 。这一切都很顺利。

当触摸屏幕时,使用touchesBegan,我想暂停当前动画并使用touchesMoved事件更改UIView的垂直位置。在 TouchesEnded 中,我想将动画恢复到所需的结束位置。

这样做的正确方法是什么?

托马斯

最佳答案

我在 UIView 上创建了一个类别来暂停和停止动画:

@interface UIView (AnimationsHandler)

- (void)pauseAnimations;
- (void)resumeAnimations;

@end

@implementation UIView (AnimationsHandler)
- (void)pauseAnimations
{
CFTimeInterval paused_time = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.layer.speed = 0.0;
self.layer.timeOffset = paused_time;
}

- (void)resumeAnimations
{
CFTimeInterval paused_time = [self.layer timeOffset];
self.layer.speed = 1.0f;
self.layer.timeOffset = 0.0f;
self.layer.beginTime = 0.0f;
CFTimeInterval time_since_pause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time;
self.layer.beginTime = time_since_pause;
}

关于iphone - 如何暂停和恢复UIView动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3211065/

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