Turn off Zooming in UIScrollView

2019-03-17 05:56发布

问题:

Does anyone know a way to temporarily turn off zooming when using a UIScrollView?

I see that you can disable scrolling using the following:

self.scrollView.scrollEnabled = false;

but I'm not seeing a similar command for zooming. Any thoughts?

回答1:

If you want to disable the user's ability to zoom through gestures then in iOS 5 and above you can disable the pinch gesture. This still allows you to control the scroll view from code...

scrollView.pinchGestureRecognizer.enabled = NO;

similarly for pan...

scrollView.panGestureRecognizer.enabled = NO;

This must be called in - (void)viewDidAppear:(BOOL)animated or later as otherwise the system resets it to enabled.



回答2:

Following fbrereto's advice above, I created two functions lockZoom and unlockZoom. When locking Zoom i copied my max and min zoom scales to variables then set the max and min zoom scale to 1.0. Unlocking zoom just reverses the process.

-(void)lockZoom
{
    maximumZoomScale = self.scrollView.maximumZoomScale;
    minimumZoomScale = self.scrollView.minimumZoomScale;

    self.scrollView.maximumZoomScale = 1.0;
    self.scrollView.minimumZoomScale = 1.0;
}

-(void)unlockZoom
{

    self.scrollView.maximumZoomScale = maximumZoomScale;
    self.scrollView.minimumZoomScale = minimumZoomScale;

}


回答3:

Also you can return "nil" as zooming view in UIScrollViewDelegate:

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
    return canZoom?view:nil;
}


回答4:

Check setting minimumZoomScale and maximumZoomScale; According to the docs:

maximumZoomScale must be greater than minimumZoomScale for zooming to be enabled.

So, setting the values to be the same should disable zooming.



回答5:

I know this is a really old question but I made a slight variation for my purposes.

I wanted to be able to easily tell if the zooming was in fact enabled/disabled without relying on a comparison between scrollView.minimumZoomScale == scrollView.maximumZoomScale, which could possibly not reflect whether zooming was actually enabled/disabled.

So I did this

// .h
@property (assign, nonatomic, getter=isZoomEnabled) BOOL zoomEnabled;

// .m
@synthesize zoomEnabled = _zoomEnabled;

- (void)setZoomEnabled:(BOOL)zoomEnabled;
{
  _zoomEnabled = zoomEnabled;

  UIScrollView *scrollView = self.scrollView;

  if (zoomEnabled) {
    scrollView.minimumZoomScale = self.minimumZoomScale;
    scrollView.maximumZoomScale = self.maximumZoomScale;
  } else {
    scrollView.minimumZoomScale = 0.0f;
    scrollView.maximumZoomScale = 0.0f;
  }
}

The values for self.minimumZoomScale and self.maximumZoomScale are set at the time the UIScrollView is configured.

This gives me the ability to set/ask if zooming is enabled.

myViewController.zoomEnabled = YES;
myViewController.isZoomEnabled;


回答6:

here, my solution for stop zooming on scrollview.

self.scrollView.minimumZoomScale=self.scrollView.maximumZoomScale;


回答7:

I have tried setting minimumZoomScale and maximumZoomScale properties of UIScrollView to 1 or isMultipleTouchEnabled property of UIView to false or return nil from viewForZooming(in:) of UIScrollViewDelegate but none worked. In my case, after several trial and error, the following works in my case [Tested on iOS 10.3]:

class MyViewController: UIViewController {
   var webView: WKWebView?

   override viewDidLoad() {
      super.viewDidLoad()

      //...
      self.webView.scrollView.delegate = self
      //...
   }
}

extension MyViewController: UIScrollViewDelegate { 
   func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
      scrollView.pinchGestureRecognizer?.isEnabled = false
   }
}


回答8:

If you want to disable only zooming with pinch gesture, below code does the trick.

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)


回答9:

Swift 3 Version:

func lockScrollViewZooming() {
    scrollView.minimumZoomScale = 1.0
    scrollView.minimumZoomScale = 1.0
    scrollView.bounces = false
    scrollView.bouncesZoom = false

    // Also, if you have double tap recognizer,
    // remember to remove it
    scrollView.removeGestureRecognizer(doubleTapGestureRecognizer)
}

func unlockScrollViewZooming() {
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 4.0
    scrollView.bounces = true
    scrollView.bouncesZoom = true

    // Also, if you have double tap recognizer,
    // remember to add it
    scrollView.removeGestureRecognizer(doubleTapGestureRecognizer)
}

Note that doubleTapGestureRecognizer should be an instance variable. It should be similar to:

private lazy var doubleTapGestureRecognizer: UITapGestureRecognizer = {
    let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
    doubleTapGestureRecognizer.numberOfTapsRequired = 2
    doubleTapGestureRecognizer.delegate = self

    return doubleTapGestureRecognizer
}()

@objc private func handleDoubleTap(_ recognizer: UITapGestureRecognizer) {
    //scrollView.setZoomScale((scrollView.zoomScale > scrollView.minimumZoomScale) ? scrollView.minimumZoomScale : scrollView.maximumZoomScale, animated: true)

    if scrollView.zoomScale > scrollView.minimumZoomScale {
        scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
    } else {
        let touchLocation = recognizer.location(in: recognizer.view)
        scrollView.zoom(to: CGRect(origin: touchLocation, size: CGSize(width: 22.0, height: 20.0)), animated: true)
    }
}