简体   繁体   中英

how to limit UIPanGestureRecognizer with in parent view

Hi i have used UIPanGestureRecognizer in my project. i used below code to handle UIPanGestureRecognizer

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

      CGPoint translation = [recognizer translationInView:self];
      CGFloat newX = MIN(recognizer.view.frame.origin.x + translation.x, self.frame.size.width - recognizer.view.frame.size.width);
      CGRect newFrame = CGRectMake( newX,recognizer.view.frame.origin.y, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
      recognizer.view.frame = newFrame;
      [recognizer setTranslation:CGPointZero inView:self];
}

在此处输入图片说明

i have two views in my project one is parent view(Red color view) and another one is child view (Green color view). My requirement is i need to pan the child view with in parent view. My handlePan method working fine but the only problem is its allowing to pan beyond the limit in left side. its working perfectly in right side. what i need to change in code Please help me out.

You should check UIGestureRecognizerDelegate

implement - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch and return NO when you don't want the gesture recognizer receive the touch event.

This method is called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM