Scrolling with two fingers with a UIScrollView

后端 未结 14 612
长发绾君心
长发绾君心 2020-11-29 21:29

I have an app where my main view accepts both touchesBegan and touchesMoved, and therefore takes in single finger touches, and drags. I want to im

14条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 22:08

    Check out my solution:

    #import “JWTwoFingerScrollView.h”
    
    @implementation JWTwoFingerScrollView
    
    - (id)initWithCoder:(NSCoder *)coder {
        self = [super initWithCoder:coder];
        if (self) {
            for (UIGestureRecognizer* r in self.gestureRecognizers) {
                NSLog(@“%@”,[r class]);
                if ([r isKindOfClass:[UIPanGestureRecognizer class]]) {
                    [((UIPanGestureRecognizer*)r) setMaximumNumberOfTouches:2];
                    [((UIPanGestureRecognizer*)r) setMinimumNumberOfTouches:2];
                }
            }
        }
        return self;
    }
    
    -(void)firstTouchTimerFired:(NSTimer*)timer {
        [self setCanCancelContentTouches:NO];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self setCanCancelContentTouches:YES];
        if ([event allTouches].count == 1){
            touchesBeganTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(firstTouchTimerFired:) userInfo: nil repeats:NO];
            [touchesBeganTimer retain];
            [touchFilter touchesBegan:touches withEvent:event];
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [touchFilter touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@“ended %i”,[event allTouches].count);
        [touchFilter touchesEnded:touches withEvent:event];
    }
    
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@“canceled %i”,[event allTouches].count);
        [touchFilter touchesCancelled:touches withEvent:event];
    }
    
    @end
    

    It does not delays the first touch and does not stop when the user touches with two fingers after using one. Still it allows to cancel a just started one touch event using a timer.

提交回复
热议问题