如何实现UILongPressGesture,然后在ios中滑动手势

时间:2015-07-06 12:03:45

标签: ios uiswipegesturerecognizer uilongpressgesturerecogni

我正在尝试实现LongPress手势,然后在ios上的按钮上使用Swipe Gesture。该视图更可能是WhatsApp应用程序的“Slide to Cancel”和Record功能。

目前我没有收到滑动手势事件。 我也在实现shouldRecognizeSimultaneouslyWithGestureRecognizer方法。请建议。

(BOOL)gestureRecognizer:(UIGestureRecognizer *) recognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

1 个答案:

答案 0 :(得分:0)

我们不需要单独的滑动手势识别器。 Long Press Gesture Recognizer中的不同状态可用于处理此场景。

Long Press Gesture有不同的状态,如 UIGestureRecognizerStateBegan UIGestureRecognizerStateChanged UIGestureRecognizerStateEnded

只要长按子视图,就会调用

UIGestureRecognizerStateBegan

当用户试图移动手指时,会调用

UIGestureRecognizerStateChanged

当用户从触摸点抬起手指时,会调用

UIGestureRecognizerStateEnded

- (void)longPressGestureForPreviewImageView:(UILongPressGestureRecognizer *)recognizer{

    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // Long press detected, start the timer
        [self showPreviewImage:recognizer];
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged)
    {
        NSLog(@"Swipe up");

        if ([self.thumbnailImageView.gestureRecognizers containsObject:recognizer]) {
            [self.thumbnailImageView removeGestureRecognizer:recognizer];
        }
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        [self hidePreviewImage];
    }

因此我们可以使用Gesture Delegate方法来处理滑动以及Long Press Gesture Recognizer。