[ACCEPTED]-Turn off Zooming in UIScrollView-zooming
If you want to disable the user's ability 6 to zoom through gestures then in iOS 5 and 5 above you can disable the pinch gesture. This 4 still allows you to control the scroll view 3 from code...
scrollView.pinchGestureRecognizer.enabled = NO;
similarly for pan...
scrollView.panGestureRecognizer.enabled = NO;
This must 2 be called in - (void)viewDidAppear:(BOOL)animated
or later as otherwise the 1 system resets it to enabled.
Swift 4.x and above:
imageZoomView.pinchGestureRecognizer?.isEnabled
= false / true
Following fbrereto's advice above, I created 5 two functions lockZoom and unlockZoom. When 4 locking Zoom i copied my max and min zoom 3 scales to variables then set the max and 2 min zoom scale to 1.0. Unlocking zoom just 1 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;
}
Also you can return "nil" as zooming view 1 in UIScrollViewDelegate:
- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
return canZoom?view:nil;
}
Check setting minimumZoomScale
and maximumZoomScale
; According to the docs:
maximumZoomScale
must 3 be greater thanminimumZoomScale
for zooming to be enabled.
So, setting 2 the values to be the same should disable 1 zooming.
I have tried setting minimumZoomScale
and maximumZoomScale
properties of 4 UIScrollView
to 1
or isMultipleTouchEnabled
property of UIView
to false
or return nil
from 3 viewForZooming(in:)
of UIScrollViewDelegate
but none worked. In my case, after 2 several trial and error, the following works 1 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
}
}
I know this is a really old question but 9 I made a slight variation for my purposes.
I 8 wanted to be able to easily tell if the 7 zooming was in fact enabled/disabled without 6 relying on a comparison between scrollView.minimumZoomScale == scrollView.maximumZoomScale
, which 5 could possibly not reflect whether zooming 4 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 3 values for self.minimumZoomScale
and self.maximumZoomScale
are set at the time the 2 UIScrollView
is configured.
This gives me the ability 1 to set/ask if zooming is enabled.
myViewController.zoomEnabled = YES;
myViewController.isZoomEnabled;
here, my solution for stop zooming on scrollview.
self.scrollView.minimumZoomScale=self.scrollView.maximumZoomScale;
0
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 1 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)
}
}
You need to turn off Two Fingers and Double 1 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
}
}
If you want to disable only zooming with 1 pinch gesture, below code does the trick.
scrollView.pinchGestureRecognizer?.requireGestureRecognizerToFail(scrollView.panGestureRecognizer)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.