2012-05-23 77 views
2

我正在對我的視圖執行動畫。但是我面臨的問題很少。我必須按鈕的startAnimationStop Animation。有關我的ImageView執行的動畫我正在使用startAnimation按鈕的動作下面的代碼:UIView動畫的問題

UIViewAnimationOptions option = UIViewAnimationOptionRepeat + UIViewAnimationOptionAutoreverse; 
[UIView animateWithDuration:2.5 delay:0.0 options:option 
       animations:^{ 
        CGAffineTransform scale = CGAffineTransformMakeScale(2.0, 2.0); 
        CGAffineTransform translate = CGAffineTransformMakeTranslation(-50.0, -100.0); 
        CGAffineTransform mix = CGAffineTransformConcat(scale, translate); 
        imv.transform = mix; 
       } 
       completion:^(BOOL finished) { 

       } 
]; 

這是工作。 現在我面臨的問題是:

  1. 如何阻止我stopAnimation按鈕操作這個動畫?
  2. 如果ImageView是動畫,然後我在後臺進入後臺,我再次進入前景ImageView粘在那裏,不會動畫。

如何解決這些問題?

+1

你想「停止「按鈕將圖像恢復到原始位置或暫停動畫到位? –

+0

當你重新輸入你的應用程序時,你是否期望視圖在你離開或從一開始就從原來的位置開始動畫? –

回答

5

在您的stopAnimation按鈕上,您需要從UIView圖層中移除動畫。首先你必須導入Quartzcore.framework。

#import <QuartzCore/QuartzCore.h> 


[yourView.layer removeAllAnimations]; 

至於暫停動畫,同時進入的背景和回來前景您的其他問題,世界上沒有其他辦法「暫停」的動畫。你最好的選擇是通過你的動畫restarting手動處理這種情況。

+0

問題是重新啓動annimation不起作用..直到我從超級視圖中刪除我的imageview並將其添加回 –

+0

重新啓動動畫,您可以執行以下操作 yourview.transform = CGAffineTransformIdentity; 之後,如果你重新啓動你的動畫,它將工作 – user1988

2

要停止,您可以使用

#import <QuartzCore/QuartzCore.h> 
... 

[imv.layer removeAllAnimations]; 

,作爲使動畫重新啓動它是一個複雜的問題,答案取決於你想要的應用程序時,它恢復做動畫。一個可能的答案是跟蹤你的動畫ImageView的位置,然後在ApplicationDidEnterBackground中你可以取消所有的動畫並簡單地將它移動到那裏。然後當你恢復它將在正確的位置。

如果你真的想繼續動畫,我會讀一些其他堆棧溢出的答案,看看是否有幫助。

Restoring animation where it left off when app resumes from background

iOS, Restarting animation when coming out of the background

+0

問題是重新啓動的annimation不起作用..直到我從超級視圖中刪除我的imageview並將其添加回來 –

0

有關進一步的參考,您的響應的第二部分,你必須在做任何事情之前恢復矩陣身份:

imv.transform = CGAffineTransformIdentity;