关闭 UIScrollView 中的缩放

发布于 2024-08-08 05:31:59 字数 160 浏览 5 评论 0原文

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

我发现您可以使用以下命令禁用滚动:

self.scrollView.scrollEnabled = false;

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

二智少女猫性小仙女 2024-08-15 05:32:00

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

}

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;

}
醉酒的小男人 2024-08-15 05:32:00

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

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

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

- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
    return canZoom?view:nil;
}
那一片橙海, 2024-08-15 05:32:00

检查设置 minimumZoomScalemaximumZoomScale;根据 文档

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

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

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.

后知后觉 2024-08-15 05:32:00

我尝试将 UIScrollViewminimumZoomScalemaximumZoomScale 属性设置为 1isMultipleTouchEnabled 属性UIViewfalse 或从 UIScrollViewDelegateviewForZooming(in:) 返回 nil但没有一个起作用。就我而言,经过多次尝试和错误,以下内容适用于我的情况[在 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
   }
}

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
   }
}
我喜欢麦丽素 2024-08-15 05:32:00

我知道这是一个非常古老的问题,但我出于我的目的做了一些小小的改变。

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

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;
谁对谁错谁最难过 2024-08-15 05:32:00

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

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

here, my solution for stop zooming on scrollview.

self.scrollView.minimumZoomScale=self.scrollView.maximumZoomScale;
凉宸 2024-08-15 05:32:00

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

Swift 3 Version:

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

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)
    }
}
北渚 2024-08-15 05:32:00

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

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

You need to turn off Two Fingers and Double Tap of scroll view

self.scrollView.delegate = self

And

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

   func viewForZooming(in scrollView: UIScrollView) -> UIView? {
       return nil
   }
}
云胡 2024-08-15 05:32:00

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

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)

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

scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)
乜一 2024-08-15 05:31:59

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

scrollView.pinchGestureRecognizer.enabled = NO;

与平移类似...

scrollView.panGestureRecognizer.enabled = NO;

这必须在 - (void)viewDidAppear:(BOOL)animated 或更高版本中调用,否则系统会重置它启用。

Swift 4.x 及更高版本:

imageZoomView.pinchGestureRecognizer?.isEnabled = false / true

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.

Swift 4.x and above:

imageZoomView.pinchGestureRecognizer?.isEnabled = false / true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文