【问题标题】:UIView animation being called twice, canceling out the animationUIView 动画被调用两次,取消动画
【发布时间】:2015-07-19 03:13:41
【问题描述】:

我有一个 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];
}
}

【问题讨论】:

    标签: ios cocoa-touch animation uiview uiviewanimation


    【解决方案1】:

    编辑添加您应该使用基于块的动画,例如;

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

    你可以在这里找到很多这样的例子。

    很多时候,您需要在过渡发生时禁止过渡。例如,将gesture.enabled 设置为NO,然后在动画的完成块中设置回YES,或者使用在动画开始之前设置为NO 的布尔值,然后在完成块中将其设置回YES。然后在调用动画之前检查一下该布尔值是什么。

    这类事情在 UI 实现中很常见。否则,您最终可能会像您正在经历的那样多次开火。

    【讨论】:

      猜你喜欢
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多