davetw12
davetw12

Reputation: 1841

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

Upvotes: 1

Views: 5461

Answers (3)

Senseful
Senseful

Reputation: 91681

This post highlights the three things that are necessary to enable zooming:

  1. You need a delegate that implements UIScrollViewDelegate and is set to delegate on your UIScrollView instance
  2. On your delegate you have to implement one method: viewForZoomingInScrollView: (which must return the content view you're interested in zooming). You can also implement scrollViewDidEndZooming:withView:atScale: optionally.
  3. On your UIScrollView instance, you have to set the minimumZoomScale and the maximumZoomScale to be different (they are 1.0 by default).

From your sample code, we can see #1 and #3 are implemented. Therefore, it's likely that #2 is the problem. Double check the code for implementing the viewForZoomingInScrollView method, or ideally include it in the code above.

Upvotes: 1

Rem-D
Rem-D

Reputation: 675

Two things need to be done for zooming to be enabled:

  1. Use the UIScrollViewDelegate protocol and implement viewForZoomingInScrollView:UIScrollView -> UIView?. Set the scroll bar delegate to be the implementing class. This expects the return of the UIView to be zoomed. So a simple example would be:

    
    @IBOutlet var myZoomableImageView: UIImageView?`
    
    override func viewDidLoad() {
        myScrollView?.delegate = self
    } 
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return myZoomableImageView
    }
    
  2. Set the max and min zoom to the scroll view using UIScrollView.minimumZoomScale and UIScrollView.maximumZoomScale like so:

    
    myScrollView?.minimumZoomScale = 1.0
    myScrollView?.maximumZoomScale = 3.0 
    
    `

Upvotes: 2

Pedro Cattori
Pedro Cattori

Reputation: 2805

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.

Upvotes: 2

Related Questions