Dragging and dropping UILable in UIImageview frame only Using UIPanGestureRecognizer

UILable Drag the UIView frame Not to the UIImageView frame I use the following code to drag lable using the UIPanGestureRecognizer

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture1:)]; [panGesture setMinimumNumberOfTouches:1]; [self.imageView setBounds:CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y ,self.imageView.frame.size.width, self.imageView.frame.size.height)]; [panGesture setMaximumNumberOfTouches:1]; panGesture.delegate=self; [title addGestureRecognizer:panGesture]; panGesture = nil; 

And my method of action

 -(void)handlePanGesture1:(UIPanGestureRecognizer *)sender { CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.imageView]; if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { firstX = [[sender view] center].x; firstY = [[sender view] center].y; } translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); [[sender view] setCenter:translatedPoint]; } 
+4
source share
1 answer

Well, you should limit your x, y using something like this:

  CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.frame) - (CGRectGetWidth(sender.view.bounds)/2.0f); CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.frame) - (CGRectGetHeight(sender.view.bounds) /2.0f); CGFloat minPossibleX = CGRectGetMinX(self.imageView.frame) + CGRectGetMidX(sender.view.bounds); CGFloat minPossibleY = CGRectGetMinY(self.imageView.frame) + CGRectGetMidY(sender.view.bounds); CGFloat actualX = translatedPoint.x; actualX= MAX(minPossibleX, actualX); actualX = MIN(maxPossibleX, actualX); CGFloat actualY = translatedPoint.y; actualY= MAX(minPossibleY, actualY); actualY = MIN(maxPossibleY, actualY); translatedPoint = CGPointMake(actualX, actualY); 

I assume your label does not apply to your image representation here. If your shortcut is in imageView:

  CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.bounds) - (CGRectGetWidth(sender.view.bounds)/2.0f); CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.bounds) - (CGRectGetHeight(sender.view.bounds) /2.0f); CGFloat minPossibleX = CGRectGetMidX(sender.view.bounds); CGFloat minPossibleY = CGRectGetMidY(sender.view.bounds); CGFloat actualX = translatedPoint.x; actualX= MAX(minPossibleX, actualX); actualX = MIN(maxPossibleX, actualX); CGFloat actualY = translatedPoint.y; actualY= MAX(minPossibleY, actualY); actualY = MIN(maxPossibleY, actualY); translatedPoint = CGPointMake(actualX, actualY); 

Edit: I successfully tested this method in a test project:

 -(void)handlePanGesture1:(UIPanGestureRecognizer *)sender { CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.imageView]; if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { firstX = [[sender view] center].x; firstY = [[sender view] center].y; } translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y); CGFloat maxPossibleX = CGRectGetMaxX(self.imageView.bounds) - (CGRectGetWidth(sender.view.bounds)/2.0f); CGFloat maxPossibleY = CGRectGetMaxY(self.imageView.bounds) - (CGRectGetHeight(sender.view.bounds) /2.0f); CGFloat minPossibleX = CGRectGetMidX(sender.view.bounds); CGFloat minPossibleY = CGRectGetMidY(sender.view.bounds); CGFloat actualX = translatedPoint.x; actualX= MAX(minPossibleX, actualX); actualX = MIN(maxPossibleX, actualX); CGFloat actualY = translatedPoint.y; actualY= MAX(minPossibleY, actualY); actualY = MIN(maxPossibleY, actualY); translatedPoint = CGPointMake(actualX, actualY); [[sender view] setCenter:translatedPoint]; } 
+2
source

All Articles