0

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?

denvdancsk
  • 2,995
  • 2
  • 25
  • 41
  • Have you watched this topic ?http://stackoverflow.com/questions/3657451/how-to-enable-zoom-in-uiscrollview – Roma-MT Dec 17 '13 at 18:24
  • also here is the documentation https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html – Roma-MT Dec 17 '13 at 18:27
  • Thanks but my issue is subclassing UIScrollView and getting the zoom to work. – denvdancsk Dec 17 '13 at 18:32
  • But you aren't delegating it. – Roma-MT Dec 17 '13 at 18:35
  • Exactly what I need help with! – denvdancsk Dec 17 '13 at 18:37
  • Please take a look at the voted answer here i think it is exactlly what you need http://stackoverflow.com/questions/9978279/how-to-subclass-uiscrollview-and-make-the-delegate-property-private – Roma-MT Dec 17 '13 at 18:42
  • There is a guide devoted to describing how to setup scroll view https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html – yurish Dec 17 '13 at 18:55
  • http://stackoverflow.com/questions/15670491/subclass-a-zooming-uiscrollview-without-delegate-methods – Roma-MT Dec 17 '13 at 18:58
  • So wait, it's not possible to forward the delegate to the subclass? I have to hack it? – denvdancsk Dec 17 '13 at 19:11

2 Answers2

1

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
nevan king
  • 110,877
  • 44
  • 200
  • 239
  • Thanks, I already tried self.delegate = self and also super.delegate = self but it gives the error: Assigning to 'id' from incompatible type 'ImageScrollView*__strong' – denvdancsk Dec 17 '13 at 20:09
  • Figured it out. I also had to remove the - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated method. It was conflicting with the delegate method. Thanks. – denvdancsk Dec 17 '13 at 20:33
0

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;
}
denvdancsk
  • 2,995
  • 2
  • 25
  • 41