如何暂停和恢复UIView动画? [英] How to pause and resume UIView Animation?

查看:161
本文介绍了如何暂停和恢复UIView动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个UILabel的UIView,该UILabel从上到下进行动画处理,反之亦然.某种Autoque可以说:)我使用2个功能:

I have a UIView with several UILabels that is animating from top to bottom and vice versa. A sort of Autoque let's say :) I use 2 functions:

-(void)goUp 
-(void)goDown 

这些函数将UIView动画启动到所需位置.它们都定义了 AnimationDidStopSelector ,最后调用另一个函数.这一切都顺利进行.

Those functions start a UIView animation to the required position.They both have an AnimationDidStopSelector defined that calls the other function at the end. This all works smoothly.

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

When touching the screen, using touchesBegan, I would like to pause the current animation and change the vertical position of the UIView using touchesMoved event. In touchesEnded I want to resume the animation to the required end position.

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

What would be the proper way to do so?

托马斯

推荐答案

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

I've created a category on UIView to pause and stop animations:

@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;
}

这篇关于如何暂停和恢复UIView动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆