32

有谁知道使用 UIScrollView 时暂时关闭缩放的方法?

我看到您可以使用以下方法禁用滚动:

self.scrollView.scrollEnabled = false;

但我没有看到类似的缩放命令。有什么想法吗?

4

10 回答 10

48

如果您想禁用用户通过手势缩放的能力,那么在 iOS 5 及更高版本中,您可以禁用捏合手势。这仍然允许您从代码控制滚动视图...

scrollView.pinchGestureRecognizer.enabled = NO;

同样对于泛...

scrollView.panGestureRecognizer.enabled = NO;

这必须在- (void)viewDidAppear:(BOOL)animated或以后调用,否则系统会将其重置为启用。

Swift 4.x 及更高版本:

imageZoomView.pinchGestureRecognizer?.isEnabled=/

于 2013-12-18T23:35:20.137 回答
30

按照上面 fbrereto 的建议,我创建了两个函数 lockZoom 和 unlockZoom。锁定缩放时,我将最大和最小缩放比例复制到变量,然后将最大和最小缩放比例设置为 1.0。解锁缩放只会逆转这个过程。

-(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;

}
于 2009-10-13T17:17:32.243 回答
17

您也可以在 UIScrollViewDelegate 中返回“nil”作为缩放视图:

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
    return canZoom?view:nil;
}
于 2011-03-18T21:11:51.533 回答
11

检查设置minimumZoomScalemaximumZoomScale;根据文档

maximumZoomScale必须大于minimumZoomScale才能启用缩放。

因此,将值设置为相同应该禁用缩放。

于 2009-10-13T17:00:32.040 回答
5

我尝试过设置minimumZoomScalemaximumZoomScale属性UIScrollViewto1或to或 return from isMultipleTouchEnabledof但没有奏效。在我的情况下,经过多次试验和错误,以下在我的情况下有效[在 iOS 10.3 上测试]:UIViewfalsenilviewForZooming(in:)UIScrollViewDelegate

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
   }
}
于 2017-04-05T03:45:58.443 回答
2

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;
于 2011-12-10T18:53:29.220 回答
2

在这里,我停止缩放滚动视图的解决方案。

self.scrollView.minimumZoomScale=self.scrollView.maximumZoomScale;
于 2015-07-02T10:52:25.393 回答
1

您需要关闭滚动视图的两个手指和双击

self.scrollView.delegate = self

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

   func viewForZooming(in scrollView: UIScrollView) -> UIView? {
       return nil
   }
}
于 2021-03-06T03:18:27.770 回答
1

斯威夫特 3 版本:

func lockScrollViewZooming() {
    scrollView.minimumZoomScale = 1.0
    scrollView.maximumZoomScale = 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)
}

注意doubleTapGestureRecognizer应该是一个实例变量。它应该类似于:

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)
    }
}
于 2017-03-22T12:41:33.160 回答
0

如果您只想通过捏合手势禁用缩放,下面的代码可以解决问题。

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)
于 2015-11-05T05:11:37.167 回答