Search code examples
iosobjective-cuiscrollviewuiscrollviewdelegate

UIScrollview Zoom implementation without InterfaceBuilder


I can't make zoom in UIScrollView work. So the problem is that panning is working perfectly. However, pinching and zooming doesn't work. The reason i assume is that i don't have delegates.

My approaches:

  1. I try to use delegates, and the only solution i come up with is scrollView.delegate=self

  2. I understand that i need to include something in ViewController.h, but i have no idea how to put @property UIScrollView *scrollView; and then connect it to the zoom function.

I believe that I'm on the right track, but suggestions on what to connect where is highly appreciated.

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:
    CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    scrollView.delegate = self;
    CGSize containerSize = CGSizeMake(1280, 1280);
    UIView *containerView = [[UIView alloc] initWithFrame:(CGRect)    {.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
    containerView.backgroundColor = [UIColor grayColor ];
    [scrollView addSubview:containerView];


    // Set up our custom view hierarchy
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640.0f, 80.0f)];
    redView.backgroundColor = [UIColor redColor];
    [containerView addSubview:redView];

    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 560.0f, 640.0f, 80.0f)];
    blueView.backgroundColor = [UIColor blueColor];
    [containerView addSubview:blueView];

    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(160.0f, 160.0f, 320.0f, 320.0f)];
    greenView.backgroundColor = [UIColor greenColor];
    [containerView addSubview:greenView];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slow.png"]];
    imageView.center = CGPointMake(320.0f, 320.0f);
    [containerView addSubview:imageView];
    scrollView.panGestureRecognizer.minimumNumberOfTouches=2;
    CGSize containerSize2 = CGSizeMake(640, 640);
    scrollView.contentSize=containerSize2;

    CGRect viewRect = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,  self.view.bounds.size.width, self.view.bounds.size.height);

    [self.view addSubview:scrollView];

}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that we want to zoom
    return self.containerView; //This gives error. 
}

Solution

  • Answer that solved my question: Add this code in viewDidLoad() after ViewRect()

    CGRect viewRect = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,  self.view.bounds.size.width, self.view.bounds.size.height);
    
    CGRect scrollViewFrame = scrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width;
    CGFloat scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height;
    CGFloat minScale = MIN(scaleWidth, scaleHeight);
    
    scrollView.minimumZoomScale = minScale;
    scrollView.maximumZoomScale = 1.0f;
    scrollView.zoomScale = minScale;
    

    And this how viewController.h should look like:

    @interface ViewController ()
    @property UIView *containerView;
    @end
    @implementation ViewController
    
    @synthesize containerView = _containerView;