Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: UIScrollView zooming programmatically not working

I have a pageable UIScrollView which contains different kind of informations like UITables but also zoomable images. Therefore I set up a pageable main-ScrollView and as subviews I added zoomable image-ScrollViews with the images as content. Works everything fine, just I fail to set the smaller current zoom scale of the imageScrollViews.

UIImageView *imageView = [[UIImageView alloc] initWithImage:Image];

//storing a link to the imageView
[imagelinkArray addObject:imageView];

CGRect ScrollViewImageRect;
ScrollViewImageRect = CGRectMake((self.scrollView.frame.size.width) * i, 0, 320, self.scrollView.frame.size.height);

float widthfactor = ScrollViewImageRect.size.width / imageView.frame.size.width;
float heightfactor = ScrollViewImageRect.size.height / imageView.frame.size.height;
float zoomscale = MIN(widthfactor, heightfactor);

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:ScrollViewImageRect];
imageScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);

imageScrollView.delegate = self;

[imageScrollView setMinimumZoomScale:zoomscale];
[imageScrollView setMaximumZoomScale:1.5];

[imageScrollView addSubview:imageView];

//doesn't work:
[imageScrollView setZoomScale:0.5 animated:YES];

[self.scrollView addSubview:imageScrollView];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [imagelinkArray objectAtIndex:page];
}

The main-ScrollView and the image-ScrollViews are drawn perfectly and it's possible to zoom and page properly. The minimum zoom factor is calculated also correct. When I zoom out I can zoom until the image limits are reached. However the first time I page to the image-ScrollView it's current zoom scale is always 1.0 while it should be the minimum scale.

Wherever I set the zoom scale in the code above it doesn't work:

[changeScrollView setZoomScale:changeScrollView.minimumZoomScale animated:YES];

If I log the current zoom scale i always get 1.0;

The only thing which works is changing the zoom scale in the - (void)scrollViewDidScroll: method, which of course doesn't help a lot since zooming also calls it which resets the zoom immediately. But at least I could figure out, that the code somehow works. I have the feeling a UIScrollView doesn't zoom when it's not visible on the screen right now. How can I fix this?

Update: Okay. In the meantime I figured out that the problem most likely comes from my base layout of "sub-viewing" ScrollViews into another ScrollView. When I zoom one of the images and log the current zoom factor of the ScrollViews they are all the same (main ScrollView as well as ALL sub-ScrollViews). What could be the reason for it or how could I solve it with a different layout?

like image 488
user2014551 Avatar asked Mar 24 '14 09:03

user2014551


2 Answers

I think viewForZoomingInScrollView: may not be getting called by the scrollview. Try to add a breakpoint and check what you're returning there.

like image 162
Rivera Avatar answered Nov 10 '22 18:11

Rivera


Your code doesn't work because you havent set minimumZoomScale ,which has default value of 1.0 . Since you are trying to set 0.5 which is below the default value, it wouldn't work.

Add following line just after the line which sets maximumZoomScale.

[imageScrollView setMinimumZoomScale:0.25];
like image 3
Rukshan Avatar answered Nov 10 '22 17:11

Rukshan