Capture only UIView 2 finger UIPanGestureRecognizer

橙三吉。 提交于 2019-12-10 12:56:28

问题


I have a couple of UIScrollViews in my view controller. I want to overlay a view that captures a 2 finger swipe via UIPanGestureRecognizer which will not record the UIScrollView swipe gestures.

When I put a transparent view over my content with a 2 finger pan gesture, my taps and 1 finger swipes are not detected. I tried overwriting the pointInside: method to return NO but then it doesn't record my 2 finger swipe.

The effect is similar to the 4 finger swipe to change apps.


回答1:


You don't need an overlay view.
First implement UIPanGestureRecognizer that will handle 2 finger pan and assign it to your view that contains UIScrollViews

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                initWithTarget:self 
                                                        action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];

Use UIGestureRecognizerDelegate to handle 2 finger pan with UIScrollView pan gesture

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

And finally you are able to handle 2 fingers pan

- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
    NSLog(@"pan");
}

If you want to stop scrolling UIScrollView when two finger pan is detected you can disable and enable UIScrollView pan recognizers

- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        _scrollView.panGestureRecognizer.enabled = NO;
    }
    if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        _scrollView.panGestureRecognizer.enabled = YES;
    }
    NSLog(@"pan");
}



回答2:


If you don't really need the overlay you can solve this with just gesture recognizers. I wrote this up as a test:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);

    UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
    [green setBackgroundColor:[UIColor greenColor]];

    UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
    [blue setBackgroundColor:[UIColor blueColor]];

    [_scrollView addSubview:green];
    [_scrollView addSubview:blue];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPan:)];
    [pan setMinimumNumberOfTouches:2];
    [pan setMaximumNumberOfTouches:2];
    [pan setDelaysTouchesBegan:YES];

    [_scrollView addGestureRecognizer:pan];

    [self.view addSubview:_scrollView];
}

- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture {
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            self.scrollView.scrollEnabled = NO;
            break;
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
            self.scrollView.scrollEnabled = YES;
            break;
        default:
            break;
    }
    NSLog(@"2 Fingers!");
}

I get the twoFingerPan: call back for when 2 fingers are used. The scroll view's panGestureRecognizer is still working at that point so I disable scrolling on the scroll view to handle the 2 finger pan. I've found this method work's pretty well. One sort of wonky thing is if the scroll view is decelerating the 2 finger gesture recognizer isn't called. Hope that helps!



来源:https://stackoverflow.com/questions/16301286/capture-only-uiview-2-finger-uipangesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!