2010-07-09 138 views
4

我有一個UIView,有幾個UILabels,從上到下動畫,反之亦然。 A排序Autoque的假設:)我用2個功能:如何暫停和恢復UIView動畫?

-(void)goUp 
-(void)goDown 

這些功能啓動一個UIView動畫所需的position.They都有一個AnimationDidStopSelector定義來電結束的其他功能。這一切工作順利。

當觸摸屏幕,採用的touchesBegan,我想暫停當前動畫和更改使用touchesMoved事件的UIView的垂直位置。在touchesEnded中,我想將動畫恢復到所需的最終位置。

什麼是這樣做的正確方法?

托馬斯

+0

http://stackoverflow.com/questions/9104487/how-to-pause-and-resume-uiview-animation-無塊動畫是我最好的工作方式。 – 2012-02-02 02:53:20

回答

1

我不知道是否有可能與UIView的的直接,但你絕對可以做,動畫視圖的CALayers代替。有關暫停和恢復CAAnimations的信息,請參閱this question

2

弗拉基米爾,關於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。這將取消原有的動畫,並把動畫視圖不是它的目標位置(它會自動完成),但在動畫理線的當前位置..

希望這會幫助別人呢! :)

+0

pausedX是什麼?我把它設置爲一個int,它可以工作。 – 2012-02-01 18:43:40

+0

如果您將pausedX定義爲int,它會靜默地轉換爲CGFloat。 frameStop是一個CGRect,它是一個由CGSize和CGPoint組成的結構。起點是由兩個CGFloats組成的結構。 – willc2 2012-05-20 19:26:32

+0

如果您正在爲變形而非框架設置動畫,您如何做到這一點? – zakdances 2012-12-18 23:10:36

4

其實,你仍然可以暫停基於該問題的答案是弗拉基米爾鏈接到它暫停我CABasicAnimations以及我UIView動畫我已經實現我所有的動畫作爲CABasicaAnimations後,然後添加一些UIView動畫之後我認爲不會暫停的動畫,但它們也不起作用。 This is the relevant link

我想暫停我的整個看法,所以我通過self.view.layer如要暫停該層。但對於那些不知道CALayer的人,請通過view.layer,您想暫停。每個UIView有一個CALayer,所以只需傳遞最相關的view.layer。在托馬斯的情況下,根據你自己的回答,似乎你想通過self.containerView.layer暫停。

,這個工作的原因是因爲UIView動畫只是在核心動畫之上的一層。至少這是我的理解。

希望這有助於未來人們想知道如何暫停動畫。

0

希望它能幫助你。

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

當您調用圖層動畫時,它會影響所有圖層樹和子圖層動畫。

7

我已經創建了一個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; 
} 
+0

建議檢查resumeAnimations if paused_time == 0以確保我們在之前暫停了它 – 2015-06-10 16:15:24

+0

此方法似乎與UIPercentDrivenInteractiveTransition不兼容。 – Johan 2016-06-06 12:00:46