views:

908

answers:

1

I have added several UIScrollViews as subviews of a single UIView and set the frames so that each one is clearly visable. I set scrollEnabled to YES and set the contentSize larger than the bounds/frame. I do this in a for loop, and with each pass of the loop I release the UIScrollView (though the object is still stored because it has been subviewed into the UIView). This works well for being able to scroll around the imageView stored in each particular UIScrollView but I cannot for the life of me figure out how to get the zoom to work. I included the in the interface. Here are the methods I have tried for choosing the correct view for zooming:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [[myView subviews] objectAtIndex:pageNum];
}

and

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [myView viewWithTag:pageNum];
}

neither seems to work. The weird part is that scrolling works fine. I can't even get the viewForZooming method to get called at all if I put in an NSLog call. Any ideas? I think I've lost all my hair from getting frustrated with this.

Edit: Thanks a lot cduhn! All I needed was that little bump, I had forgotten to set the scrollView delegate to self... I've been working with various apps that take advantage of UIScrollView for months now and been using the delegate correctly and this most recent one I don't know where my brain went.

However, you do not need to override the scrollViewDidEndZooming:withView:atScale:, the delegate will call that no matter what after a zoom.

Also, after a little tweeking this worked:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [[[myView viewWithTag:pageNum] subviews] objectAtIndex:0];
}

This simply calls the scrollView inside View container and then gets the UIImage inside of that... works well.

+1  A: 

It sounds like you may not have set the delegate property on your UIScrollViews to point at the object that implements viewForZoomingInScrollView:

Also note this snippet from the UIScrollView Class Reference:

For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.

Finally, a word of warning: Be careful when accessing the subviews of UIScrollView. Your subviews are not alone in there. UIScrollView adds its own UIImageViews as subviews of itself to implement its scrollbar UI. So code like this...

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [[myView subviews] objectAtIndex:pageNum]; 
}

... may not do what you expect.

cduhn