簡體   English   中英

iOS UIView動畫取消不起作用

[英]iOS UIView Animation cancel doesn't work

我有兩個圖像mosha1和mosha2。 我通過UIView動畫將它們從某個位置移動到另一個點(206,89)。 現在,我想如果用戶在移動時觸摸任何圖像,則該圖像的移動將在那里停止。 因此,我設置了停止動畫運動的代碼:

[mosha1.layer removeAllAnimations];

此方法調用停止了圖像的移動,但是隨后在點(206,89)中顯示了該圖像。 但是它應該出現在用戶觸摸的確切位置。 如何解決呢?

UIView動畫方法:

-(void)moshaMoveUp: (UIImageView *)mosha {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
mosha.center = CGPointMake(206, 89);
[UIView commitAnimations];

}

這樣做的原因是動畫如何在不同的圖層上工作。 開始動畫的那一刻,您的對象在邏輯上立即位於目標位置。 但是,有一個表示層,顯示對象向目的地移動。 因此,如果要停止對象,則需要從UIView的表示層中獲取其當前位置,然后將其分配給視圖的位置,然后刪除動畫,然后將對象放置在正確的位置。

停止動畫后,該frame將成為最終目的地。 如果您想將其停在原處,則必須獲取視圖圖層的presentationLayer (其中包含對象的當前狀態,而不是最終狀態),然后使用它來調整相關的視圖frame

CALayer *layer = self.animatedView.layer.presentationLayer;
[self.animatedView.layer removeAllAnimations];
self.animatedView.frame = layer.frame;

改用它。 您正在執行的操作非常古老,不再受支持。

[UIView animateWithDuration:3.0
                      delay:0.0
                    options:UIViewAnimationOptionsCurveEaseInOut
                 animations:^() {
                             mosha.center = CGPointMake(206, 89);
                            }
                 completion:nil];

這可能會或可能不會解決您的取消問題,但是您需要使用這種動畫方法。

我知道這不能解決問題。 但是使用最新和不推薦使用的方法仍然很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM