Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
I've written some code to ensure an image only pans when it has zoomed in (with a pinch gesture) and the image does not pan off screen when it has zoomed in.

Can anyone offer any advice about refactoring this?

i couldn't use the scroll view delegate methods as they were incompatible with my rotation gesture logic.

Code:
-(void)panGestureRecognized:(UIPanGestureRecognizer*)recognizer {
    
    if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged)
    {
        CGFloat currentScale = [[recognizer.view.layer valueForKeyPath:@"transform.scale"] floatValue];
        if (currentScale > 1.00) {
            
            CGPoint translation = [recognizer translationInView:recognizer.view.superview];
            CGPoint imageCenter = recognizer.view.center;
            
            CGPoint newCenter = CGPointMake(imageCenter.x + translation.x, imageCenter.y + translation.y);
            
            [recognizer.view setCenter:newCenter];
            [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
        }
    }
    
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGFloat currentScale = [[recognizer.view.layer valueForKeyPath:@"transform.scale"] floatValue];
        if (currentScale > 1.00)
        {
            CGRect superViewFrame = recognizer.view.superview.layer.frame;
            CGRect imageViewFrame = recognizer.view.layer.frame;
            
            CGFloat offscreenContentX = (superViewFrame.size.width - imageViewFrame.size.width);
            CGFloat offscreenContentY = (superViewFrame.size.height - imageViewFrame.size.height);
            
            //Right
            if (imageViewFrame.origin.x >= 0) {
                //Down
                if (imageViewFrame.origin.y >= 0) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(0, 0, imageViewFrame.size.width, imageViewFrame.size.height)];
                        //Up
                } else if (imageViewFrame.origin.y <= offscreenContentY) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(0, offscreenContentY, imageViewFrame.size.width, imageViewFrame.size.height)];
                    //Neither
                } else {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(0, imageViewFrame.origin.y, imageViewFrame.size.width, imageViewFrame.size.height)];
                }
                //Left
            } else  if (imageViewFrame.origin.x <= offscreenContentX) {
                //Down
                if (imageViewFrame.origin.y >= 0) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(offscreenContentX, 0, imageViewFrame.size.width, imageViewFrame.size.height)];
                } else if (imageViewFrame.origin.y <= offscreenContentY) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(offscreenContentX, offscreenContentY, imageViewFrame.size.width, imageViewFrame.size.height)];
                } else {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(offscreenContentX, imageViewFrame.origin.y, imageViewFrame.size.width, imageViewFrame.size.height)];
                }
            } else {
                if (imageViewFrame.origin.y >= 0) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(imageViewFrame.origin.x, 0, imageViewFrame.size.width, imageViewFrame.size.height)];
                } else if (imageViewFrame.origin.y <= offscreenContentY) {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(imageViewFrame.origin.x, offscreenContentY, imageViewFrame.size.width, imageViewFrame.size.height)];
                } else {
                    [self moveView:recognizer.view backOnScreenWithFrame:CGRectMake(imageViewFrame.origin.x, imageViewFrame.origin.y, imageViewFrame.size.width, imageViewFrame.size.height)];
                }
            }
        }
    }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.