Question

I have a UIViewController, which calls a UIView:

I am using UIPinchGesture to zoom into the UIView What I want to do is limit how much a user can pan, according to the zoomed Scale

i.e. "currentScale"

Currently the code I'm using allows no panning, when the currentScale (Amount zoomed) is less than 1.1x zoomed, but if it is great that 1.1 it allows pannin, but this allows the UIView to be panned and moved about without bounds, I want to be able to set its panning amount to its boundaries : current code

if (currentScale <= 1.1f) {
    // Use this to animate the position of your view to where you want
    [UIView animateWithDuration: 0.5
                          delay: 0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGPoint finalPoint = CGPointMake(self.view.bounds.size.width/2,
                                                          self.view.bounds.size.height/2);
                         recognizer.view.center = finalPoint; }
                     completion:nil];
}

else {
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointZero inView:self.view];
}

Some direction, would be very appreciated -thank you !

Was it helpful?

Solution

DISCLAIMER -This Probably isn't the best way to do this, but this is how i solved it:

1) I deduce how much I would need to pan in the X 0r Y direction at 5 different zooming points, by measuring how the center of the view deviated from its original position:

2) I used NSLog for most of the measurements) - I standardized the results - and plot it in excel - graphed a curve - and got an equation for Zoom level Vs View.center

3) Then i simply coded the panning gesture in accordance to the equations I got:

The code is below (xMax, xMin, yMax, yMin all have plotted equation with the common factor being the "zoomScale"

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

//dont pan if zoomscale = 1 (this indicates no zooming)
if (zoomScale <= 1.0f) {
    return;
}

//panning gesture began / state changes 
if ([recognizer state] == UIGestureRecognizerStateBegan ||
    [recognizer state] == UIGestureRecognizerStateChanged) {

    //detect translation gesture 
    translation = [recognizer translationInView:self.view];
    //newCenter is a variable detecting how your translation gesture would efect your view's center 
    CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x,
                                    recognizer.view.center.y + translation.y);

    //Check whether boundary conditions are met 
    BOOL inBounds = (newCenter.y >= yMin && newCenter.y <= yMax &&
                     newCenter.x >= xMin && newCenter.x <= xMax);

    if  (inBounds) {
        //if boundary conditions met : translate your view 
        recognizer.view.center = newCenter;
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
} 

Hope this could help somebody out there: Not you must declare you must initiate (declare) UIPanGestureRecognizer in your viewDidLoad method for this to work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top