1

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

  1. 长按创建黑色方块
  2. 手指停留在屏幕上
  3. 拖动方形视图 (setCenter:)
  4. 滑动识别 UISwipeGestureRecognizer

问题是在长按手势未完成时我无法识别滑动(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;
}

PS 当您通过长按在日画布上创建事件时,我希望其行为与本机日历应用程序中的行为完全相同。如果您滑动对象(在结束长按手势之前),它会平滑消失。

4

2 回答 2

1

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

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

这是文档的链接

编辑

So I tried doing the same thing and, Yes even in my case the swipe was not recognized until long press is over..! So fi you want to move the view around, THen use PanGesture instead of swipe gesture..! It will work I just tried..:)

于 2012-10-04T08:39:14.653 回答
0

您使用 enabled:NO 关闭滑动识别器。我建议保持它。并计算例如识别一个手势的次数。如果收到相同的手势,则不要添加此计数器。当它以正确的顺序发生时,您设置一个函数来执行打开/释放操作。

于 2012-10-04T07:56:39.207 回答