Search code examples
iosuiscrollviewuiimagefirst-responder

UIScrollview, Move down responder chain


I'll try and keep it simple.

I have a UIScrollview with around 10 images attached. I currently have it so that i can touch an image and drag it around on the scroll view.

I did this by creating the subclass UIImageview and implementing the touchesMoved etc. I can still scroll the view fine, but the problem comes when trying to drag an image too fast. It seems the program first checks if the view is being scrolled and then fires touchesMoved in the UIImage class.

Is there anyway I can switch this around so that the first check is if an image is touched, then if not pass the response onto the scrollview.

Any help would be great. Thanks.


Solution

  • The simplest way to do this would be use one finger to move an image, and two fingers to scroll the view.

    If you're on iOS 5, this is super easy:

    self.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;
    

    If you want to support older versions of iOS, you have to do a little more work:

    for (UIGestureRecognizer *gesture in self.scrollView.gestureRecognizers){
      if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]){
        ((UIPanGestureRecognizer *)gesture).minimumNumberOfTouches = 2;
      }
    }
    

    If you want to use one-finger gestures for both, there are a few ways to do it. You could attach a UIPanGestureRecognizer to each image view. You might need to tell the scroll view's own UIPanGestureRecognizer to defer to the image view recognizers, using the requireGestureRecognizerToFail: message.

    Another way would be to set the scroll view's UIPanGestureRecognizer's delegate to an object you create that implements the gestureRecognizer:shouldReceiveTouch: method. In that method, you can check whether the touch's view is one of your image views. If so, return NO to prevent the scroll view's pan gesture recognizer from activating.