Scrolling with two fingers with a UIScrollView

后端 未结 14 631
长发绾君心
长发绾君心 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:24

    we managed to implement similar functionality in our iPhone drawing app by subclassing UIScrollView and filtering events depending on number of touches in simple and rude way:

    //OCRScroller.h
    @interface OCRUIScrollView: UIScrollView
    {
        double pass2scroller;
    }
    @end
    
    //OCRScroller.mm
    @implementation OCRUIScrollView
    - (id)initWithFrame:(CGRect)aRect {
        pass2scroller = 0;
        UIScrollView* newv = [super initWithFrame:aRect];
        return newv;
    }
    - (void)setupPassOnEvent:(UIEvent *)event {
        int touch_cnt = [[event allTouches] count];
        if(touch_cnt<=1){
            pass2scroller = 0;
        }else{
            double timems = double(CACurrentMediaTime()*1000);
            pass2scroller = timems+200;
        }
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self setupPassOnEvent:event];
        [super touchesBegan:touches withEvent:event];
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [self setupPassOnEvent:event];
        [super touchesMoved:touches withEvent:event];   
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        pass2scroller = 0;
        [super touchesEnded:touches withEvent:event];
    }
    
    
    - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
    {
        return YES;
    }
    
    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
    {
        double timems = double(CACurrentMediaTime()*1000);
        if (pass2scroller == 0 || timems> pass2scroller){
            return NO;
        }
        return YES;
    }
    @end
    

    ScrollView setuped as follows:

    scroll_view = [[OCRUIScrollView alloc] initWithFrame:rect];
    scroll_view.contentSize = img_size;
    scroll_view.contentOffset = CGPointMake(0,0);
    scroll_view.canCancelContentTouches = YES;
    scroll_view.delaysContentTouches = NO;
    scroll_view.scrollEnabled = YES;
    scroll_view.bounces = NO;
    scroll_view.bouncesZoom = YES;
    scroll_view.maximumZoomScale = 10.0f;
    scroll_view.minimumZoomScale = 0.1f;
    scroll_view.delegate = self;
    self.view = scroll_view;
    

    simple tap does nothing (you can handle it in the way you need), tap with two fingers scrolls/zooms view as expected. no GestureRecognizer is used, so works from iOS 3.1

提交回复
热议问题