为UIScrollView子类设置缩放

时间:2013-12-17 18:17:57

标签: ios uiscrollview ios7 subclass uiscrollviewdelegate

我正在尝试将UIScrollView子类化为“ ImageScrollView ”。我的课非常简单。它有一个imageView作为我希望该类缩放到的子视图之一。

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

添加子类作为我的viewcontroller的子视图,除了滚动之外还可以正常工作:

#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

我的第一个猜测是,它与未正确实现子类的委托有关。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

放大UIScrollView

的基础知识
  1. 设置滚动视图委托(确保您设置为委托的课程符合UIScrollViewDelegate。在您的情况下,这类似于self.delegate = self
  2. 设置minimumZoomScalemaximumZoomScale(或两者)
  3. 实施viewForZoomingInScrollView:以返回要缩放的视图

答案 1 :(得分:0)

想出来。我不得不添加委托声明并删除 - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated。如果有人需要,这就是答案:

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;
}