Turn off Zooming in UIScrollView

2019-03-17 05:32发布

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?

9条回答
来,给爷笑一个
2楼-- · 2019-03-17 05:54

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-17 05:55

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;
查看更多
爷、活的狠高调
4楼-- · 2019-03-17 06:03

here, my solution for stop zooming on scrollview.

self.scrollView.minimumZoomScale=self.scrollView.maximumZoomScale;
查看更多
Root(大扎)
5楼-- · 2019-03-17 06:08

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

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
    return canZoom?view:nil;
}
查看更多
乱世女痞
6楼-- · 2019-03-17 06:14

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;

}
查看更多
倾城 Initia
7楼-- · 2019-03-17 06:16

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

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)
查看更多
登录 后发表回答