简体   繁体   中英

How to zoom a UIImageView within a UIScrollView?

I have a UIImageView within a UIScrollView and I can vertically scroll, but not horizontal scroll because of how I set the content size. That's exactly how I want.

However, I can't seem to zoom in and out. I set the minimum and maximum zoom scale, but it's not working.

Can someone tell me why?

Thanks.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //self.scrollView.scrollEnabled = YES;

    CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.scrollView setContentSize:scrollableSize];

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
    tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.minimumZoomScale = 1.0  ;
    self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
    self.scrollView.zoomScale = 1.0;
    self.scrollView.delegate = self;

    [self.scrollView addSubview:tempImageView];
}

You have to implement the following delegate method for zooming:

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

and make sure that you add self.imageView to your self.scrollView instead. It should look like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    [self.scrollView setContentSize:scrollableSize];

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"] ];
    self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);

    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.minimumZoomScale = 1.0  ;
    self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
    self.scrollView.delegate = self;

    [self.scrollView addSubview:self.imageView];
}

To get zooming to work, you need to provide a delegate method that returns the view you wish to get zoomed:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
   return self.imageView
}

You will also need to hold on assign tempImageView to a property so that you can refer to it here. If you don't want to hang on to it in a property, you will need some other way of identifying the view, eg scrollView.subviews[0] if it is the only subview.

Try this Try this

[scroll setUserInteractionEnabled:YES];

And your Maximum zoom scale is strange. Try with 2,0 to test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM