Set zoom for UIScrollView subclass

552 views Asked by At

I am trying to subclass UIScrollView as "ImageScrollView". My class is pretty simple. It has an imageView as one of it's subviews that I want the class to zoom to.

ImageScrollView.h

#import <UIKit/UIKit.h>

@interface ImageScrollView : UIScrollView

@property (nonatomic, strong) UIImageView *imageView;

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image;

@end

ImageScrollView.m

#import "ImageScrollView.h"

@interface ImageScrollView()

@property CGFloat scaleWidth;
@property CGFloat scaleHeight;

@end

@implementation ImageScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image
{
    self = [super init];
    if(self)
    {
        self.frame = frame;

        self.imageView = [[UIImageView alloc]initWithImage:image];
        [self addSubview:self.imageView];

        self.contentSize = self.imageView.bounds.size;

        self.scaleWidth = self.frame.size.width / image.size.width;
        self.scaleHeight = self.frame.size.height / image.size.height;
        CGFloat minScale = MIN(self.scaleWidth, self.scaleHeight);

        self.maximumZoomScale = 1.0f;
        self.minimumZoomScale = minScale;
        [self setZoomScale:minScale];

        [self zoomToRect:self.imageView.frame animated:NO];
    }
    return self;
}


@end

Adding the subclass as a subview of my viewcontroller works just fine except for the scrolling:

#import "ViewController.h"
#import "ImageScrollView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    ImageScrollView *imageScrollView = [[ImageScrollView alloc]initWithFrame:self.view.bounds andImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:imageScrollView];
  }

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

@end

My first guess is that it has something to do with the delegate not implemented correctly for the subclass. Any suggestions?

2

There are 2 answers

0
denvdancsk On BEST ANSWER

Figured it out. I had to add the delegate declaration and remove the - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated. Here's the answer in case anyone needs it:

ImageScrollView.h

#import <UIKit/UIKit.h>

@interface ImageScrollView : UIScrollView <UIScrollViewDelegate>

@property (nonatomic, strong) UIImageView *imageView;

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image;

@end

ImageScrollView.m

#import "ImageScrollView.h"

@interface ImageScrollView()

@property CGFloat scaleWidth;
@property CGFloat scaleHeight;

@end

@implementation ImageScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image
{
    self = [super init];
    if(self)
    {
        self.frame = frame;
        self.delegate = self;

        self.imageView = [[UIImageView alloc]initWithImage:image];
        [self addSubview:self.imageView];

        self.contentSize = self.imageView.bounds.size;

        self.scaleWidth = self.frame.size.width / image.size.width;
        self.scaleHeight = self.frame.size.height / image.size.height;
        CGFloat minScale = MIN(self.scaleWidth, self.scaleHeight);

        self.maximumZoomScale = 1.0f;
        self.minimumZoomScale = minScale;
        [self setZoomScale:minScale];
    }
    return self;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}
2
nevan king On

Basics for zooming in a UIScrollView

  1. Set scroll view delegate (making sure the class you set as delegate conforms to UIScrollViewDelegate. In your case this is something like self.delegate = self
  2. Set minimumZoomScale or maximumZoomScale (or both)
  3. Implement viewForZoomingInScrollView: to return view to zoom