关闭在缩放的UIScrollView(Turn off Zooming in UIScrollVie

2019-08-17 23:41发布

有谁知道一种方式来暂时关闭使用一个UIScrollView变焦时?

我看到,你可以使用以下禁用滚动:

self.scrollView.scrollEnabled = false;

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

Answer 1:

如果要禁用用户通过手势进行放大,然后在iOS 5及以上可以禁用缩放手势能力。 这仍然可以让你控制从代码滚动视图...

scrollView.pinchGestureRecognizer.enabled = NO;

同样,对于泛...

scrollView.panGestureRecognizer.enabled = NO;

这必须在被调用- (void)viewDidAppear:(BOOL)animated或更高版本作为否则系统重置为启用。

斯威夫特4.x及以上:

imageZoomView.pinchGestureRecognizer?.isEnabled = /



Answer 2:

按照上面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;

}


Answer 3:

您还可以返回“零”作为UIScrollViewDelegate缩放视图:

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


Answer 4:

检查设置minimumZoomScalemaximumZoomScale ; 根据该文档 :

maximumZoomScale必须大于minimumZoomScale用于变焦启用。

因此,在设置这些值是相同的,应禁用变焦。



Answer 5:

我知道这是一个很老的问题,但我做了,我的目的稍有不同。

我想,如果变焦实际上不依靠之间的比较启用/禁用,以便能够很容易地分辨scrollView.minimumZoomScale == scrollView.maximumZoomScale ,这有可能不能反映是否变焦实际上是启用/禁用。

所以我做了这个

// .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;
  }
}

为值self.minimumZoomScaleself.maximumZoomScale设定在时间UIScrollView配置。

这给我设置/询问是否启用变焦的能力。

myViewController.zoomEnabled = YES;
myViewController.isZoomEnabled;


Answer 6:

在这里,我的一站式解决方案缩放滚动视图上。

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


Answer 7:

斯威夫特3版本:

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)
}

需要注意的是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)
    }
}


Answer 8:

我已经尝试设置minimumZoomScalemaximumZoomScale的性质UIScrollView1isMultipleTouchEnabled财产UIView ,以false或返回nilviewForZooming(in:)UIScrollViewDelegate但没有奏效。 在我的情况下,一些试错后,在我的情况下工作[测试在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
   }
}


Answer 9:

如果您希望禁用只捏合手势缩放,下面的代码做的伎俩。

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)


文章来源: Turn off Zooming in UIScrollView