Why does panning allow the user to move the enlarged view outside the supervisor?

I am working on creating and expanding a function on pdf pages. My pinch and pan (move) work correctly, but when the user constantly moves the enlarged image, the zoom view goes beyond the super-view. Something like that:

theis is the move user made after zoom

how can I limit the panning movement so that the user cannot move the enlarged view / pdf outside the supervisor.
The corresponding code I use is:

// This method will handle the PINCH / ZOOM gesture 

- (void) pinchZoom: (UIPinchGestureRecognizer *) gestureRecognizer {

  if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { // Reset the last scale, necessary if there are multiple objects with different scales lastScale = [gestureRecognizer scale]; } if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { if (!zoomActive) { zoomActive = YES; panActive = YES; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMove:)]; [panGesture setMaximumNumberOfTouches:2]; [panGesture setDelegate:self]; [self addGestureRecognizer:panGesture]; [panGesture release]; } CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue]; // Constants to adjust the max/min values of zoom const CGFloat kMaxScale = 2.0; const CGFloat kMinScale = 1.0; CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); newScale = MIN(newScale, kMaxScale / currentScale); newScale = MAX(newScale, kMinScale / currentScale); CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale); [gestureRecognizer view].transform = transform; lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call [delegate leavesView:self zoomingCurrentView:[gestureRecognizer scale]]; } 

}
method in which I handle the pan movement:

 // This method will handle the PAN / MOVE gesture - (void)panMove:(UIPanGestureRecognizer *)gestureRecognizer { if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) { CGPoint translation = [gestureRecognizer translationInView:[[gestureRecognizer view] superview]]; [[gestureRecognizer view] setCenter:CGPointMake([[gestureRecognizer view] center].x + translation.x, [[gestureRecognizer view] center].y + translation.y)]; [gestureRecognizer setTranslation:CGPointZero inView:[[gestureRecognizer view] superview]]; } } 

Please suggest how to handle panning / moving, restricting panning within the observation boundaries.

+4
source share
2 answers

Try this code in your panMove method. His work is beautiful in my case.

  static CGPoint initialCenter; if (recognizer.state == UIGestureRecognizerStateBegan) { initialCenter = recognizer.view.center; } CGPoint translation = [recognizer translationInView:recognizer.view]; CGPoint newCenter = CGPointMake(initialCenter.x + translation.x, initialCenter.y + translation.y); CGRect newFrame = recognizer.view.frame; CGRect superViewBounds = recognizer.view.superview.bounds; CGPoint superViewOrigin = recognizer.view.superview.frame.origin; if(newCenter.x-(newFrame.size.width/2) >= (superViewBounds.size.width+superViewOrigin.x)-200 /*right*/ || newCenter.x+(newFrame.size.width/2) <= (superViewOrigin.x+200) /*left*/ || newCenter.y-(newFrame.size.height/2) >= (superViewBounds.size.height+superViewOrigin.y)-200 /*bottom*/ || newCenter.y+(newFrame.size.height/2) <= (superViewOrigin.y+100)) /*top*/ { return; }else{ recognizer.view.center = newCenter; } 
+2
source

You can add something like this to the code: (This is a little rude, so you may have to work out some errors)

 if([gestureRecognizer view].frame.bounds.x < self.view.bounds.x - panExapantion) { //then don't move it } //... repeat this for all sides (this is left), bottom, right, and top. 

Edit: Okay, consider the box inside the drawers, so we have an inner box and an outer box. If we do not want the inner box to go outside the outer field, we must have all of these statements:

  • The moved left side of the indoor unit is not outside the left side of the external box.
  • Moving the right side of the indoor unit is not outside the right side of the outer box.
  • The moved bottom side of the indoor unit is not outside the bottom side of the outer box.
  • The moved top side of the indoor unit is not behind the top side of the outer box.

In your case, PDF is the inner box, and the iPad is the outer box. To stop the publication of PDF code outside the field, we need to check whether each of these statements is true, and if it is false, we do not move the PDF to a new location. Or we move the PDF document a little closer to the edge of the iPhone. screen.

The problem is that the pinch and scaling are used, and suddenly the box will always be outside the outer box, so how do we fix it? We get how many pixels were added to the inner box when it was enlarged (for the bag of this explanation, you can simply call this extension). Thus, we get how the box was expanded and subtracted this value. For example: (This is an if statement and does not work in code)

 If(outerBox.leftSide is less than innerBox.leftSide - panExpantion) { //Then the innerBox is outside the outterBox } 

I was hoping this helped clarify!

0
source

Source: https://habr.com/ru/post/1416364/


All Articles