2011-12-05 48 views
1

我目前有一個默認的循環動畫,當視圖出現時淡入淡出,當數據的背景處理完成時,它將停止當前動畫加載新的動畫,根據新的圖片。如何停止循環動畫並用新值再次調用

我不能看到停止使用[self.view.layer removeAllAnimations];[self.imageViewBottom.layer removeAllAnimations];[self.imageViewTop.layer removeAllAnimations];

-(void)nextAnimation:(float)previousWidth { 

//picture loop 
imageViewTop.image = imageViewBottom.image; 
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1]; 

[imageArray insertObject:imageViewBottom.image atIndex:0]; 
[imageArray removeLastObject]; 
imageViewTop.alpha = 1.0; 
imageViewBottom.alpha = 0.0; 

[UIView animateWithDuration:4.0 
       animations:^{ 
        imageViewTop.alpha = 0.0; 
        imageViewBottom.alpha = 1.0; 
        } 
       completion:^(BOOL completed){ 
        [self nextAnimation:stringsize.width]; 
       } 
]; 



-(void)foundSetup 
{ 
    //[imageViewTop removeFromSuperview]; 
    //[imageViewBottom removeFromSuperview]; 
    //[buttonCaption removeFromSuperview]; 
    [self.view.layer removeAllAnimations]; 
    [self.imageViewBottom.layer removeAllAnimations]; 
    [self.imageViewTop.layer removeAllAnimations]; 


    //[self.imageArray removeAllObjects]; 
    //self.imageArray = foundImageArray; 
} 
+3

'[imageViewTop.layer removeAllAnimations];'和'[imageViewBottom.layer removeAllAnimations];'? – Till

+1

@Till,你應該將其作爲答案發布。 self.view沒有動畫,因此刪除它們毫無意義。 – jrturton

+0

感謝直到,還沒有動畫 – Desmond

回答

3
[UIView animateWithDuration:4.0     
    animations:^{      
    imageViewTop.alpha = 0.0;      
    imageViewBottom.alpha = 1.0;      
    }     
    completion:^(BOOL completed){      
    [self nextAnimation:stringsize.width];     
    } 

這裏,在完成塊中,無論動畫是否被取消,您都將再次調用下一個動畫方法。

裏面你完成塊,只能打電話nextAnimation如果完成== YES:

completion:^(BOOL completed){      
if (completed)   
    [self nextAnimation:stringsize.width];     
    } 

這一點,再加去除實際被動畫的意見動畫由截止建議,應該爲你做。

+0

真棒.....我錯過了這....感謝Jrturton。 – Desmond

1

動畫您可以用計時器去,以及創建一個定時器對象,並調用通過對象這個功能一旦烏爾後臺處理得到了無效對象[boothTimer1無效]並再次調用。在我的一個應用程序中,我已經完成了類似的場景。在.h中聲明boothTimer1

NSTimer * boothTimer1; self.boothTimer1 = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(ur function)userInfo:nil repeatats:NO];

現在你可以在你的後臺處理完成時使計時器無效。

+0

你將如何做到這一點,你介意分享一些示例代碼?因爲我使用的是位置,所以時間可能每次都不一樣。 – Desmond

+0

如果iOS是20年前建立的系統,則可以使用此類解決方案。幸運的是,它不是爲穴居人而建的,因此使用計時器對象與強制重繪是不合適的。 – Till

+0

@Till:如果是這種情況,那麼它應該已被蘋果棄用。 – iamsult