在UIScrollView中关闭缩放

时间:2009-10-13 16:53:47

标签: ios iphone uiscrollview zoom zooming

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

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

self.scrollView.scrollEnabled = false;

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

10 个答案:

答案 0 :(得分:41)

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

scrollView.pinchGestureRecognizer.enabled = NO;
同样适用于潘......

scrollView.panGestureRecognizer.enabled = NO;

必须在- (void)viewDidAppear:(BOOL)animated或更高版本中调用此方法,否则系统会将其重置为已启用。

答案 1 :(得分:29)

根据上面的fbrereto建议,我创建了两个函数lockZoom和unlockZoom。锁定Zoom时,我将最大和最小缩放比例复制到变量,然后将最大和最小缩放比例设置为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;

}

答案 2 :(得分:16)

此外,您可以在UIScrollViewDelegate中将“nil”作为缩放视图返回:

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

答案 3 :(得分:9)

检查设置minimumZoomScalemaximumZoomScale;根据{{​​3}}:

  

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

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

答案 4 :(得分:2)

我知道这是一个非常古老的问题,但我的目的略有不同。

我希望能够轻松判断缩放是否实际上已启用/禁用,而不依赖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;

答案 5 :(得分:2)

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

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

答案 6 :(得分:1)

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

答案 7 :(得分:1)

我尝试将minimumZoomScale的{​​{1}}和maximumZoomScale属性设置为UIScrollView1属性isMultipleTouchEnabledUIView或者从false nil返回viewForZooming(in:)但没有效果。在我的情况下,经过多次试验和错误,以下工作在我的情况下[在iOS 10.3上测试]:

UIScrollViewDelegate

答案 8 :(得分:0)

如果你想仅使用捏合手势禁用缩放,下面的代码可以解决问题。

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)

答案 9 :(得分:0)

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

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