2015-07-19 74 views
-1

我有一個UIView動畫從左邊帶來一個UIView到屏幕上,該方法正在快速連續地從條形碼掃描引擎觸發,該方法正在觸發然後再次觸發,因此它從屏幕上移開。UIView動畫被調用兩次,取消動畫

-(IBAction)showDetailModeView 
{ 
    if (detailModeViewON==FALSE) 
    { 
     [UIView beginAnimations:@"move buttons" context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [detailModeView setFrame:CGRectMake(0, 0, 320, 568)]; 
     [UIView commitAnimations]; 

     detailModeViewON=TRUE; 
    } 
    else 
    { 
     [UIView beginAnimations:@"move buttons" context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [detailModeView setFrame:CGRectMake(-320, 0, 320, 568)]; 
     [UIView commitAnimations]; 

     [self stopScanning]; 
     scannedONCE = FALSE; 
     detailModeViewON=FALSE; 
    } 
    [self.view endEditing:YES]; 
} 

是否有某種方式我基本上可以說,如果這個方法在最後5秒內被調用,不要做任何事情。

僞代碼

-(IBAction)showDetailModeView 
{ 

if(UIVIEW animation was triggered longer than 5 seconds ago) 
{ 
    if (detailModeViewON==FALSE) 
    { 
     [UIView beginAnimations:@"move buttons" context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [detailModeView setFrame:CGRectMake(0, 0, 320, 568)]; 
     [UIView commitAnimations]; 

     detailModeViewON=TRUE; 
    } 
    else 
    { 
     [UIView beginAnimations:@"move buttons" context:nil]; 
     [UIView setAnimationDuration:.3]; 
     [detailModeView setFrame:CGRectMake(-320, 0, 320, 568)]; 
     [UIView commitAnimations]; 

     [self stopScanning]; 
     scannedONCE = FALSE; 
     detailModeViewON=FALSE; 
    } 
    [self.view endEditing:YES]; 
} 
} 

回答

1

被修改以添加應使用基於塊的動畫,例如;

[UIView animateWithDuration:(NSTimeInterval)duration 
       animations:(void (^)(void))animations 
       completion:(void (^)(BOOL finished))completion]; 

你可以在這裏找到很多這方面的例子。

很多時候你需要禁止一個正在發生的轉換。例如,將gesture.enabled設置爲NO,並在動畫的完成塊中返回YES,或者在動畫開始之前使用設置爲NO的布爾值,並在完成塊中將其設置回YES。然後在調用動畫之前檢查布爾值是什麼。

這些類型的東西在UI實現中很常見。否則,您可能會遇到許多像您所遇到的多次觸發事件。