Dave Chambers
Dave Chambers

Reputation: 2563

How to Zoom a UIImageView that fills the screen

I'm simply trying to zoom a photo I have set on a UIImageView that is on a UIScrollView. Perhaps it's because the image is the same size as the screen but in my code below neither viewForZoomingInScrollView nor scrollViewDidZoom are called. Please help me fix this so I can zoom around the image. Thanks:

Interface:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView* scrollView;
@property (strong, nonatomic) UIImageView *imageHolder;

@end

Implementation:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.view.frame.size;

self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;

_imageHolder = [[UIImageView alloc]initWithFrame:self.view.frame];
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

[self.imageHolder setImage:[UIImage imageNamed:@"P1020486.png"]];

[self.scrollView addSubview:_imageHolder];
[self.view addSubview:self.scrollView];

//Keep the proportion and fit the screen, not filling it --> when we rotate, resizes image correctly:
_imageHolder.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
//
self.scrollView.minimumZoomScale = minScale;
self.scrollView.maximumZoomScale = 1.0f;

[self centerScrollViewContents];
}

- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageHolder.frame;

if (contentsFrame.size.width < boundsSize.width) {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
    contentsFrame.origin.x = 0.0f;
}

if (contentsFrame.size.height < boundsSize.height) {
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
    contentsFrame.origin.y = 0.0f;
}

self.imageHolder.frame = contentsFrame;
}

#pragma mark - UIScrollViewDelegate

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

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Upvotes: 0

Views: 625

Answers (2)

CodeBySteveZ
CodeBySteveZ

Reputation: 116

From looking at your code real quick, it looks like you're setting properties on the scrollView before you've initialized it:

self.scrollView.contentSize = self.view.frame.size;

self.scrollView.delegate = self;

self.scrollView.scrollEnabled = YES;

_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

If you reverse the order, it should work.

Upvotes: 2

Alois Holub
Alois Holub

Reputation: 145

I have no time to test it, but I think that problem is this line:

self.scrollView.maximumZoomScale = 1.0f;

Try to put there 2.0f or something bigger.

Upvotes: 3

Related Questions