Search code examples
xcodeuipangesturerecognizeruipinchgesturerecognizer

Pan gesture view with uncrossable boarder


To easy understand my question without a picture:

  1. I have self.view in bgcolor: black

  2. I have view1 the same big as self.view in bgcolor: white.

  3. I have pinch and pan gesture in view1.

So what I need, is to zoom and slide a view around, but I should not see the black color of self.view. So for example the right border of view1 can maximal be shifted to the right boarder of self.view. I hope this is easy to understand. What I have till now:

- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture {
    CGPoint translation = [panGesture translationInView:panGesture.view.superview];

    if (UIGestureRecognizerStateBegan == panGesture.state ||UIGestureRecognizerStateChanged == panGesture.state) {
        panGesture.view.center = CGPointMake(panGesture.view.center.x + translation.x,
                                             panGesture.view.center.y + translation.y);
        [panGesture setTranslation:CGPointZero inView: self.view];
    }
}

Solution

  • Subclass UIPanGestureRecognizer and override translationInView to confine its value as desired.