如何在手势处于活动状态时启动手势识别器?

时间:2016-05-18 10:10:03

标签: ios iphone uiswipegesturerecognizer uilongpressgesturerecogni

我在我的superview中添加了一个Subview A(self.thumbnailImageView)。我已将UILongPressGestureRecognizer和UISwipeGestureRecognizer添加到我的子视图A.

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

 if (recognizer.state == UIGestureRecognizerStateBegan)
 {
    // Long press detected, start the timer
    [self showPreviewImage:recognizer];
 }
 else if(recognizer.state == UIGestureRecognizerStateEnded)
 {
    [self hidePreviewImage];
 }
}

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

    UIImageView *bigPreviewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Desktop"]];

    bigPreviewImage.frame = CGRectMake(self.thumbnailImageView.frame.origin.x - 50.0, self.thumbnailImageView.frame.origin.y + self.thumbnailImageView.frame.size.height + 10.0, 300.0, 250.0);
    bigPreviewImage.tag = 10000;//200.0 & 125.0
    [bigPreviewImage setUserInteractionEnabled:YES];

    [self.view addSubview:bigPreviewImage];
} 

现在在处理程序方法中,当UILongPressGestureRecognizer状态开始时,我将一个Subview B(bigPreviewImage)添加到我的superview(self.view)。

shouldRecognizeSimultaneouslyWithGestureRecognizer

现在,一旦UILongPressGestureRecognizer处于活动状态,用户仍然按下子视图A,如果用户向上滑动屏幕,我希望SwipeGestureRecognizer启动。但同样的事情并没有发生。如何在手势处于活动状态时启动手势识别器?

我已经实现了var logoImage = SKSpriteNode(imageNamed: "image1.png") 方法,但仍未调用向上滑动手势方法。如果我错过了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

得到了!!!

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

Long Press Gesture有不同的状态,如UIGestureRecognizerStateBeganUIGestureRecognizerStateChangedUIGestureRecognizerStateEnded

    只要您长按子视图,
  • 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手势识别器。