UIView动画被调用两次,取消动画
问题描述:
我有一个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];
}
}
答
被修改以添加应使用基于块的动画,例如;
[UIView animateWithDuration:(NSTimeInterval)duration
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion];
你可以在这里找到很多这方面的例子。
很多时候你需要禁止一个正在发生的转换。例如,将gesture.enabled设置为NO,并在动画的完成块中返回YES,或者在动画开始之前使用设置为NO的布尔值,并在完成块中将其设置回YES。然后在调用动画之前检查布尔值是什么。
这些类型的东西在UI实现中很常见。否则,您可能会遇到许多像您所遇到的多次触发事件。