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