2014-05-07 61 views
1

我使用下面的代碼創建動畫暫停圖像:我想從一個數組

NSMutableArray *dashBoy = [NSMutableArray array]; 
for (int i = 1; i<= 20; i++) { 
    butterfly = [NSString stringWithFormat:@"5c_%d.jpg", i]; 
    if ((image = [UIImage imageNamed:butterfly])) 
     [dashBoy addObject:image]; 
    } 

    [stgImageView setAnimationImages:dashBoy]; 
    [stgImageView setAnimationDuration:7.0f]; 
    [stgImageView setAnimationRepeatCount:-1]; 
    [stgImageView startAnimating]; 

我的要求是如果dashBoy是5c_10.jpg,然後暫停圖像進行約5秒,並然後恢復動畫,如果dashBoy是5c_20.jpg,則再次暫停圖像約5秒鐘並重新恢復。可能嗎?

回答

2

您不能使用像這樣的UIImageView動畫變速。

雖然有幾種方法。

1.自己動手。

爲此,您可以使用類似NSTimer的東西,並讓它反覆激發一種方法來爲您更改圖像。使用此可以每個時間圖像單獨地(你甚至可以創建包含的圖像和時間的長度來顯示它,然後創建與這些陣列的數據對象。

2.操縱當前方法。

如果你有20幅圖像,你要告訴他們所有的7秒鐘,然後這就是......每幅圖像0.35秒。所以5秒的暫停約14圖像相當。

因此,而不是將每個圖像一次您可以添加1-9一次,然後添加10-14次。11-19一次,然後20-14次。

然後它會像它正在做的那樣交換,但是當它達到10時,它會將它交換爲同一圖像的另一個副本,以便它看起來像暫停。

然後,您必須將持續時間...增加到17秒,以獲得每個圖像的相似持續時間。

我該怎麼辦?

雖然它聽起來像是一個黑客(因爲它),我想我會給第二個方法先去。工作起來要容易得多,所以如果失敗了,你還沒有花很長時間來完成工作。

第一種方法是設置更多的工作,但會允許更好地控制動畫。

快速的方法1

創建一個對象像髒例子...

MyTimedImage 
------------ 
UIImage *image 
CGFloat duration 

因此,例如...

// probably want this as a property 
NSMutableArray *timedImages = [NSMutableArray array]; 

MyTimedImage *timedImage = [MyTimedImage new]; 
timedImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"5c_%d.jpg", i]]; 
timedImage.duration = 0.4; 

[timedImages addObject:timedImage]; 

然後你想辦法顯示他們...

//set up properties something like this... 
@property (nonatomic, assign) NSInteger currentIndex; 
@property (nonatomic, assign) BOOL paused; 
@property (nonatomic, assign) NSTimer *imageTimer; 

- (void)displayNextImage 
{ 
    if (self.paused) { 
     return; 
    } 

    NSInteger nextIndex = self.currentIndex + 1; 

    if (nextIndex == [self.timedImages count]) { 
     nextIndex = 0; 
    } 

    MyTimedImage *nextImage = self.timedImages[nextIndex]; 

    self.currentIndex = nextIndex;  

    self.imageView.image = nextImage.image; 

    self.imageTimer = [NSTimer scheduledTimerWithInterval:nextImage.duration target:self selector:@selector(displayNextImage) userInfo:nil repeats:NO]; 
} 

使用您公開的屬性可以暫停按鈕按下(例如)當前圖像上的圖像視圖。

要啓動該過程,只需運行[self displayNextImage];,您也可以在圖像循環中的任意位置啓動。

+0

你可以舉例說明第一種方法 – user3571918

+0

編輯我的答案。 – Fogmeister

1

使用UIImageView動畫,這是不可能的,您可能需要創建自己的動畫邏輯,如加載數組中的所有UIImages並通過計時器,切換到下一個動畫幀,並且當您希望暫停時,使計時器失效。

相關問題