[英]How to pause a UIImageView animation
我有一個動畫,我正在使用UIImageView顯示:
imageView.animationImages = myImages;
imageView.animationDuration = 3;
[imageView startAnimating];
我知道我可以使用stopAnimating來阻止它,但我想要的是能夠暫停它。 原因是當你調用stop時,你的動畫圖像都沒有顯示出來,而我希望最后一個在我按下按鈕時保持最新狀態。
我已經嘗試將持續時間設置為更大的數字,但這會導致所有圖像也消失。 必須有一個非常基本的方法來做到這一點?
此代碼將在動畫過程中暫停動畫對象的當前位置。 如果你記錄其他變量,如時間或進度或你需要的任何東西,再次恢復動畫應該是相當簡單的。
UIView *viewBeingAnimated = //your view that is being animated
viewBeingAnimated.frame = [[viewBeingAnimated.layer presentationLayer] frame];
[viewBeingAnimated.layer removeAllAnimations];
//when user unpauses, create new animation from current position.
嗯...因為似乎沒有人知道我猜這是不可能的。
我繼續編寫自己的UIView
,使用UIImageView
subview
,使用NSTimer
在圖像之間切換。 這樣做的好處是我可以在閑暇時暫停和恢復計時器,性能似乎不是問題。
@oddmeter只是一點編輯:
animatedView.animationImages = images; //images is your array
[animatedView startAnimating];
//Then when you need to pause;
[animatedView stopAnimating]; //Important!!
animatedView.image = [images objectAtIndex: 0];
這應該可以解決問題: https : //developer.apple.com/library/ios/#qa/qa2009/qa1673.html
它基本上告訴您需要做什么來暫停/恢復任何基於CALayer
的動畫。
如果您對UIImageView
控制的動畫使用CALayer
方法感到不舒服,您可以隨時自己制作基於UIImage
數組的動畫。 所需的代碼很短,您可以從這里獲取: http : //rssv2.blogspot.com/2011/04/animating-series-of-images-with-calayer.html
另一個選項是設置圖像屬性以及animationImages
屬性。 當UIImageView
的動畫停止時,執行此操作將顯示靜態圖像。
假設您的類是UIImageView
的子類並且具有NSMutableArray
圖像,請執行以下操作:
self.animationImages = images;
//yes, I'm skipping the step to check and make sure you have at least one
//element in your array
self.image = [images objectAtIndex: 0];
也許你可以截取最后一個動畫圖像的截圖並顯示出來?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.