iOS: Can I override pinch in/out behavior of UIScrollView?

前端 未结 4 1839
轮回少年
轮回少年 2020-12-19 13:21

I\'m drawing a graph on a UIView, which is contained by a UIScrollView so that the user can scroll horizontally to look around the entire graph.

4条回答
  •  失恋的感觉
    2020-12-19 13:59

    Although you cannot delete the existing pinch gesture recognizer, you can disable it and then add your own:

    // Disable existing recognizer
    for (UIGestureRecognizer* recognizer in [_scrollView gestureRecognizers]) {
        if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
            [recognizer setEnabled:NO];
        }
    }
    
    // Add our own
    UIPinchGestureRecognizer* pinchRecognizer = 
      [[UIPinchGestureRecognizer alloc] initWithTarget:self 
                                                action:@selector(pinch:)];
    [_scrollView addGestureRecognizer:pinchRecognizer];
    [pinchRecognizer release];
    

    Then in

    - (void) pinch:(UIPinchGestureRecognizer*)recognizer { .. }
    

    use

    [recognizer locationOfTouch:0 inView:..]
    [recognizer locationOfTouch:1 inView:..]
    

    to figure out if the user is pinching horizontally or vertically.

提交回复
热议问题