如何暂停和恢复UIView动画?
我有一个UIView,有几个UILabels,从上到下动画,反之亦然。 A排序Autoque的假设:)我用2个功能:如何暂停和恢复UIView动画?
-(void)goUp
-(void)goDown
这些功能启动一个UIView动画所需的position.They都有一个AnimationDidStopSelector定义来电结束的其他功能。这一切工作顺利。
当触摸屏幕,采用的touchesBegan,我想暂停当前动画和更改使用touchesMoved事件的UIView的垂直位置。在touchesEnded中,我想将动画恢复到所需的最终位置。
什么是这样做的正确方法?
托马斯
弗拉基米尔,关于CAAnimations问题意义..但我找到了一种方法来“暂停”这样我就可以继续使用的UIView动画:
CALayer *pLayer = [self.containerView.layer presentationLayer];
CGRect frameStop = pLayer.frame;
pausedX = frameStop.origin.x;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.01];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// set view properties
frameStop.origin.x = pausedX;
self.containerView.frame = frameStop;
[UIView commitAnimations];
我在做什么在这里使用表示层找取出动画视图的当前x值。之后,我执行一个覆盖原始动画的新动画。确保setAnimationBeginsFromCurrentstate:YES。这将取消原有的动画,并把动画视图不是它的目标位置(它会自动完成),但在动画理线的当前位置..
希望这会帮助别人呢! :)
其实,你仍然可以暂停基于该问题的答案是弗拉基米尔链接到它暂停我CABasicAnimations
以及我UIView
动画我已经实现我所有的动画作为CABasicaAnimations
后,然后添加一些UIView
动画之后我认为不会暂停的动画,但它们也不起作用。 This is the relevant link。
我想暂停我的整个看法,所以我通过self.view.layer
如要暂停该层。但对于那些不知道CALayer
的人,请通过view.layer
,您想暂停。每个UIView
有一个CALayer
,所以只需传递最相关的view.layer
。在托马斯的情况下,根据你自己的回答,似乎你想通过self.containerView.layer
暂停。
,这个工作的原因是因为UIView
动画只是在核心动画之上的一层。至少这是我的理解。
希望这有助于未来人们想知道如何暂停动画。
希望它能帮助你。
- (void)goUP{
CFTimeInterval pausedTime = [self.layer timeOffset];
self.layer.speed = 1.0;
self.layer.timeOffset = 0.0;
self.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
self.layer.beginTime = timeSincePause;
}
- (void)goDown{
CFTimeInterval pausedTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
self.layer.speed = 0.0;
self.layer.timeOffset = pausedTime;
}
当您调用图层动画时,它会影响所有图层树和子图层动画。
我已经创建了一个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;
}
建议检查resumeAnimations if paused_time == 0以确保我们在之前暂停了它 – 2015-06-10 16:15:24
此方法似乎与UIPercentDrivenInteractiveTransition不兼容。 – Johan 2016-06-06 12:00:46
http://stackoverflow.com/questions/9104487/how-to-pause-and-resume-uiview-animation-无块动画是我最好的工作方式。 – 2012-02-02 02:53:20