I have a little problem showing an image in my UIScrollView, I have this code to show images in a paged scroll view like the Photos app and works like a charm:
- (void)createView
{
dValue = [dConfiguration objectForKey:@"Buttons"];
int xAxis = 0;
int iTag = 0;
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenRect.size.width, screenRect.size.height)];
NSString *imageName = @"";
for (NSString* sProperty in dValue)
{
imageName = [util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]];
CGRect imageFrame = CGRectMake(xAxis + ((screenRect.size.width - 320) / 2), 0, 320, screenRect.size.height);
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:imageFrame];
[self LoadImage:tempImageView imageName:imageName];
tempImageView.userInteractionEnabled = YES;
tempImageView.contentMode = UIViewContentModeScaleAspectFit;
xAxis += self.view.frame.size.width;
[uiView addSubview:tempImageView];
iTag++;
}
self.pcPage.hidden = NO;
[self.pcPage setNumberOfPages:iTag];
self.pcPage.pageIndicatorTintColor = PAGECONTROL_TINTCOLOR;
self.pcPage.currentPageIndicatorTintColor = PAGECONTROL_CURRENTPAGE;
[_svScroll setContentSize:CGSizeMake(xAxis, 0)];
[_svScroll addSubview:uiView];
}
But now I want to add the zoom functionality so I have changed my code to this and I have added an extra UIScrollView.
like they say here: How to zoom in/out an UIImage object when user pinches screen?:
- (void)createView
{
dValue = [dConfiguration objectForKey:@"Buttons"];
int xAxis = 0;
int iTag = 0;
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenRect.size.width, screenRect.size.height)];
NSString *imageName = @"";
for (NSString* sProperty in dValue)
{
imageName = [util getImageName:[sProperty valueForKeyPath:@"Image"] andRetrina:[sProperty valueForKeyPath:@"ImageRetina"]];
CGRect imageFrame = CGRectMake(xAxis + ((screenRect.size.width - 320) / 2), 0, 320, screenRect.size.height);
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:imageFrame];
[self LoadImage:tempImageView imageName:imageName];
tempImageView.userInteractionEnabled = YES;
tempImageView.contentMode = UIViewContentModeScaleAspectFit;
xAxis += self.view.frame.size.width;
UIScrollView *uiScrollView = [[UIScrollView alloc] initWithFrame:imageFrame];
uiScrollView.minimumZoomScale = 1.0;
uiScrollView.maximumZoomScale = 2.0;
uiScrollView.delegate = self;
[uiScrollView setContentSize:CGSizeMake(320, screenRect.size.height)];
[uiScrollView addSubview:tempImageView];
[uiView addSubview:uiScrollView];
iTag++;
}
self.pcPage.hidden = NO;
[self.pcPage setNumberOfPages:iTag];
self.pcPage.pageIndicatorTintColor = PAGECONTROL_TINTCOLOR;
self.pcPage.currentPageIndicatorTintColor = PAGECONTROL_CURRENTPAGE;
[_svScroll setContentSize:CGSizeMake(xAxis, 0)];
[_svScroll addSubview:uiView];
}
This code is only showing the first image and I don't know what I'm doing wrong.
I tough at first that maybe the new UIScrollView was not in the proper position, so I changed the backround color (in random way) of the UIScrollView and I can see the 2 scroll well paged in the main scroll:
iColor = 50;
[uiScrollView setBackgroundColor:[UIColor colorWithRed:iColor/255.0 green:131.0/255.0 blue:19.0/255.0 alpha:1]];
iColor += 50;
Then I tough that the view UIImageView is not un the UIView and I tested it:
UIImageView *v1 = [[[[uiView subviews] objectAtIndex:0] subviews] objectAtIndex:0];
UIImageView *v2 = [[[[uiView subviews] objectAtIndex:1] subviews] objectAtIndex:0];
UIScrollView *v3 = [[uiView subviews] objectAtIndex:0];
UIScrollView *v4 = [[uiView subviews] objectAtIndex:1];
UIImageView *v5 = [[[[[[_svScroll subviews] objectAtIndex:0] subviews] objectAtIndex:0] subviews] objectAtIndex:0];
UIImageView *v6 = [[[[[[_svScroll subviews] objectAtIndex:0] subviews] objectAtIndex:1] subviews] objectAtIndex:0];
Nothing here is null and I can even see the image with the quick view function.
If I print the values this are the results:
Printing description of v1:
UIImageView: 0x156d6b90; frame = (0 0; 320 480); opaque = NO; layer = CALayer: 0x156d4810
Printing description of v2:
UIImageView: 0x156dc0f0; frame = (320 0; 320 480); opaque = NO; layer = CALayer: 0x156bdc90
Printing description of v3:
UIScrollView: 0x1639a000; frame = (0 0; 320 480); clipsToBounds = YES; gestureRecognizers = NSArray: 0x1567a060; layer = CALayer: 0x156d5c90; contentOffset: {0, 0}; contentSize: {320, 480}
Printing description of v4:
UIScrollView: 0x15b7d800; frame = (320 0; 320 480); clipsToBounds = YES; gestureRecognizers = NSArray: 0x155dae10; layer = CALayer: 0x155de3d0; contentOffset: {0, 0}; contentSize: {320, 480}
Printing description of v5:
UIImageView: 0x156d6b90; frame = (0 0; 320 480); opaque = NO; layer = CALayer: 0x156d4810
Printing description of v6:
UIImageView: 0x156dc0f0; frame = (320 0; 320 480); opaque = NO; layer = CALayer: 0x156bdc90
As you can see it looks like the frames and the content size are ok, but for some reason I can only see the first image.
I have also tested to change the image order to see if it was something in the image but the result is the same.
I have also added this code to control the zoom:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [[scrollView subviews] objectAtIndex:0];
}
And this to calculate the page:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = self.svScroll.frame.size.width;
int page = floor((self.svScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pcPage.currentPage = page;
}
So at the end, the first image works perfect, I can zoom in and out, I can scroll to the next page and I can scroll to the next page even if I have zoomed in the image, but for some reason the second image is not working (or any except the first one).
Here you have some screen shots: