Search code examples
iosuiscrollviewuiscrollviewdelegate

UIScrollView is not zooming


I'm having issue with the zooming feature of UIScrollView. It does not work at all. I have read similar posts and none of the fixes others used seem to work for me. My viewController references UIScrollViewDelegate, I have an instance variable for UIScrollView, I declare my subview to be the delegate object of UIScrollView, and I have the implementation for (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView. Here's my loadView function:

(void)loadView 
{
    CGRect frame = CGRectMake(0, 0, 320, 480);

    scrollView = [[UIScrollView alloc] initWithFrame:frame];
    [scrollView setScrollEnabled:YES];

    CGRect reallyBigRect;
    reallyBigRect.origin = CGPointZero;
    reallyBigRect.size.width = frame.size.width * 2.0;
    reallyBigRect.size.height = frame.size.height * 2.0;
    [scrollView setContentSize:reallyBigRect.size];

    CGPoint offset;
    offset.x = frame.size.width * 0.5;
    offset.y = frame.size.height * 0.5;
    [scrollView setContentOffset:offset];


    [scrollView setMinimumZoomScale:1];
    [scrollView setMaximumZoomScale:5];
    [scrollView setUserInteractionEnabled:YES];
    self.scrollView.delegate = self;


    self.view = scrollView;

    views = [[HypnosisView alloc] initWithFrame:reallyBigRect];

    [scrollView addSubview:views];

    [self.view setBackgroundColor:[UIColor clearColor]]; 

}

This code allows me to scroll just fine but zooming is nonexistent.

Hope to hear from someone, Dave


Solution

  • Have you tried implementing scrollViewDidEndZooming:withView:atScale:?


    From Apple's UIScrollView documentation:

    A scroll view also handles zooming and panning of content. As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should should update subviews of the content as necessary. (Note that the gesture can end and a finger could still be down.) While the gesture is in progress, the scroll view does not send any tracking calls to the subview.

    The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. 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.