同时使用UILongPressGestureRecognizer和UISwipeGestureRecognizer

时间:2012-10-04 07:25:18

标签: objective-c ios

我使用长按手势创建一个简单的视图(黑色方块)并将其拖动到屏幕上(UIGestureRecognizerStateChanged),直到我捕获到UIGestureRecognizerStateEnded。好的,这很好用。现在我希望能够开始识别这个黑色方形视图的附加手势(UISwipeGestureRecognizer)。算法很简单:

  1. 长按
  2. 创建黑色方块
  3. 手指停留在屏幕上
  4. 拖动方形视图(setCenter:)
  5. 滑动并识别UISwipeGestureRecognizer
  6. 问题是当长按手势未完成时我无法识别滑动(UIGestureRecognizerStateEnded)

    #pragma mark - View lifecycle
    
    - (void)loadView {
        [super loadView];
    
        // Long press
        self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
        [self.longPressGestureRecognizer setDelegate:self];
        [self.view addGestureRecognizer:self.longPressGestureRecognizer];
    
        // Swipe
        self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
        [self.swipeGestureRecognizer setDelegate:self];
        [self.swipeGestureRecognizer setEnabled:NO];
        [self.view addGestureRecognizer:self.swipeGestureRecognizer];
    }
    
    #pragma mark - Gesture recognizer
    
    - (void)gestureAction:(UIGestureRecognizer *)recognizer {
    
        // Swipe
        if ([recognizer isKindOfClass:NSClassFromString(@"UISwipeGestureRecognizer")]) {
            NSLog(@"swipeGestureRecognizer handled!!");
        }
    
        // Long press
        if ([recognizer isKindOfClass:NSClassFromString(@"UILongPressGestureRecognizer")]) {
            if (recognizer.state == UIGestureRecognizerStateBegan) {
                self.activeView = [[ItemView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
                [self.activeView setBackgroundColor:[UIColor blackColor]];
                self.activeView.center = [recognizer locationInView:self.view];
                [self.view addSubview:self.activeView];
            }
            else if (recognizer.state == UIGestureRecognizerStateChanged) {
                if (![self.swipeGestureRecognizer isEnabled]) {
                    [self.swipeGestureRecognizer setEnabled:YES];
                    NSLog(@"swipeGestureRecognizer enabled");
                }
                self.activeView.center = [recognizer locationInView:self.view];
            }
            else if (recognizer.state == UIGestureRecognizerStateEnded) {
                [self.swipeGestureRecognizer setEnabled:NO];
                NSLog(@"swipeGestureRecognizer disabled");
                [self.activeView removeFromSuperview];
                self.activeView = nil;
            }
        }
    }
    
    #pragma mark - UIGestureRecognizerDelegate
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        return YES;
    }
    
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
        return YES;
    }
    

    P.S。当您在日期画布上长按时创建事件时,我希望行为与本机日历应用程序中的行为完全相同。如果你滑动一个物体(在你长按手势之前)它会平滑地消失。

2 个答案:

答案 0 :(得分:1)

要同时识别多个手势,您需要使用UiGestureRecognizer协议中的以下委托方法,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

以下是文档的LINK

修改

所以我尝试做同样的事情,是的,即使在我的情况下,直到长按结束才能识别滑动..!所以你想要移动视图,然后使用PanGesture而不是滑动手势..!它会工作我刚试过.. :))

答案 1 :(得分:0)

您启用滑动识别器时关闭:否。 我建议坚持下去。 并且例如计算一个手势被识别的时间。 如果收到相同的手势,则不要添加此计数器。当它以正确的顺序发生时,你设置一个功能来打开/释放一个动作。