iOS允许在一个给定时间触摸一个事件

时间:2012-02-23 20:04:25

标签: ios cocoa-touch animation

我正在使用iOS 5来实现应用。

在这个应用程序中我有4个按钮,每个按钮触发动画以取消隐藏UIView。但是,如果我按一个按钮然后按另一个按钮,则首先出现的视图将消失,并且将出现新按钮的视图。

到目前为止,我有这个工作。但是如果用户快速点击两个按钮,它将显示两个视图。我怎样才能确保只处理一次触摸事件?

按钮的操作类似于:

- (void)buttonPressed:(id)sender
{
    MenuButton *aButton = (MenuButton *)sender;
    switch (aButton.tag) {
        case 0:
            if (displayingView && currentlyDisplayingView != picker1)
                [self toggleView:currentlyDisplayingView atIndex:currentIndex];
            [self toggleView:picker1 atIndex:aButton.tag];
            currentlyDisplayingView = picker1;
            currentIndex = aButton.tag;
            break;
        case 1:
            if (displayingView && currentlyDisplayingView != picker2)
                [self toggleView:currentlyDisplayingView atIndex:currentIndex];
            [self toggleView:picker2 atIndex:aButton.tag];
            currentlyDisplayingView = picker2;
            currentIndex = aButton.tag;
            break;
        case 2:
            if (displayingView && currentlyDisplayingView != picker3)
                [self toggleView:currentlyDisplayingView atIndex:currentIndex];
            [self toggleView:picker3 atIndex:aButton.tag];
            currentlyDisplayingView = picker3;
            currentIndex = aButton.tag;
            break;
        default:
            break;
    }
    NSLog(@"Pressed %@",[buttonNames objectAtIndex:aButton.tag]);
}

动画代码:

- (void)toggleView:(UIView *)picker
             atIndex:(NSInteger)index
{
    if (picker) {
        picker.hidden = NO;
        [UIView animateWithDuration:0.5 
                         animations:^{
                             picker.alpha = abs(picker.alpha - 1);
                             CGRect rect = picker.frame;
                             if (rect.size.height == 0){
                                 rect.size.height = 76;
                             } else if (rect.size.height == 76) {
                                 rect.size.height = 0;
                             }
                             picker.frame = rect;
                             for (int i = index+1; i < [viewButtons count]; i++) {
                                 UIButton *aButton = [viewButtons objectAtIndex:i];
                                 CGRect frame = aButton.frame;
                                 if (rect.size.height == 0){
                                     frame.origin.y -= 75;
                                 } else if (rect.size.height == 76) {
                                     frame.origin.y += 75;
                                 }
                                 aButton.frame = frame;
                             }
                         }
                         completion:^(BOOL success){
                             if (picker.alpha == 0){
                                 picker.hidden = YES;
                             } else if (picker.alpha == 1) {
                                 picker.hidden = NO;
                             }
                             displayingView = !displayingView;
                         }];
    }
}

1 个答案:

答案 0 :(得分:1)

在致电beginIgnoringInteractionEvents之前致电animateWithDuration,并在完成处理程序中致电endIgnoring...