同步UIView动画

7

你好,我的应用程序会响应触摸事件并进行视图动画。如果用户非常快速,并在当前动画正在进行时进行另一个触摸操作,则一切都会混乱。

框架是否提供了处理此问题的标准方法?或者我使用了错误的动画方式?

目前,它检查一个标志(animationInProgress)来处理此问题,但我想知道是否必须这样做?

以下是代码:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSMutableArray *c = (NSMutableArray*)context;
    UINavigationController *nc = [c objectAtIndex:0];
    [nc.view removeFromSuperview];
    animationInProgress = NO;
}

- (void)transitionViews:(BOOL)topToBottom {
    if (animationInProgress) {
        return;
    }
    animationInProgress = YES;
    NSMutableArray *context = [[NSMutableArray alloc] init];
    [UIView beginAnimations:@"myanim" context:context];
    [UIView setAnimationDuration:0.7f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    UIViewController *from;
    UIViewController *to;
    if (navController3.view.superview == nil ) {
        from = navController2;
        to = navController3;
    } else {
        from = navController3;
        to = navController2;
    }

    int height;
    if (topToBottom) { 
        height = -1 * from.view.bounds.size.height;
    } else {
        height = from.view.bounds.size.height;
    }

    CGAffineTransform transform = from.view.transform;

    [UIView setAnimationsEnabled:NO];
    to.view.bounds = from.view.layer.bounds;
    to.view.transform = CGAffineTransformTranslate(transform, 0, height);
    [window addSubview:to.view];

    [UIView setAnimationsEnabled:YES];
    from.view.transform = CGAffineTransformTranslate(from.view.transform, 0, -1 * height);
    to.view.transform = transform;

    [context addObject:from];

    [UIView commitAnimations];

    return;
}
1个回答

10

框架的默认行为是允许同时发生任何事情,正如您所注意到的。

如果您的目标是阻止特定动画在运行时重复发生,则像您所做的那样使用标志是完全合理的。

如果您想在动画期间从根本上停止任何触摸事件进入应用程序,则可以使用:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

与以下配对:

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

如果使用begin/endIgnoringInteractionEvents在动画期间阻止任何触摸事件进入您的应用程序,那么在此块中可能到达的触摸事件会发生什么?它们是否会被缓存并在调用endIgnoringInteractionEvents时呈现给应用程序,还是会丢弃所有触摸(如名称所示)? - vance
@vance:正如名称所示,它们将被丢弃。 - Ben Zotto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接