Reputation: 359
Hopefully someone can help with this issue. I have a class derived from UIScrollView and I'd like to prevent the user from being able to zoom or scroll via manual pinch and swipe gestures. All view navigation will instead be controlled by programmatic means in response to where a user taps (think of an ebook reader where tapping on the left or right sides of the display causes the view to scroll by exactly one page width). Any suggestions on how to implement this?
Upvotes: 8
Views: 5819
Reputation: 147
In your UIScrollView subclass overwrite also the setZoomScale: method which automatically re-disables the gesture
- (void)setZoomScale:(CGFloat)zoomScale {
[super setZoomScale:zoomScale];
self.pinchGestureRecognizer.enabled = NO;
}
Upvotes: 1
Reputation: 12087
A quick note here. It seems UIScrollView
's panGestureRecognizer
and pinchGestureRecognizer
are both enabled the first time a view controller is added to a window.
Basically what that means is setting them to enabled = NO
in viewDidLoad
won't work in some cases. I moved my enabled = NO
to viewWillAppear:
and it stuck. :)
Upvotes: 4
Reputation: 4708
On your - (void)viewDidLoad;
you should be able to just disable whatever gesture recognizer you want. In this case:
UIPinchGestureRecognizer *pinchRecognizer = self.pinchGestureRecognizer;
pinchRecognizer.enabled = NO;
or
UIPanGestureRecognizer *panRecognizer = self.scrollView.panGestureRecognizer;
panRecognizer.enabled = NO;
I sometimes do this from view controllers that contain UIScrollViews. I just target the scroll view (self.scrollView.pinchGestureRecognizer
) and temporarily disable gestures when the app. is in a certain state.
Upvotes: 9
Reputation: 359
To prevent user-controller zooming and panning but still allow programmatic zooming and panning of a scrollview, the best approach is to override the UIScrollView's -addGestureRecognizer:
method in a subclass.
In my use I wanted to block all the recognizers and control the viewable area completely from my view controller, I did so like this:
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
//Prevent any of the default panning and zooming controls from working
gestureRecognizer.enabled = NO;
[super addGestureRecognizer:gestureRecognizer];
}
Each gesture recognizer is simply disabled, for finer control (allowing the pan control but only allow zooming via a double tap for instance) you'd simply check the incoming gesture recognizer via -isKindOfClass:
and disabling as appropriate.
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
//Prevent zooming but not panning
if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
gestureRecognizer.enabled = NO;
}
[super addGestureRecognizer:gestureRecognizer];
}
I used this method in a comic reading app that uses guided navigation to animate between cropped panels on a page with the full page being contained in a UIScrollView.I can smoothly zoom in and out on a selected area by simply setting the view bounds to the region I want to display.
Upvotes: 7
Reputation: 13137
I don't have too much experience with UIScrollView
s, but looking at the docs, it looks like you can set maximumZoomScale
, minimumZoomScale
, and scrollEnabled
to disable everything you want to disable.
Here are the docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
From the docs:
scrollEnabled
:
If the value of this property is YES , scrolling is enabled, and if it is NO , scrolling is disabled. The default is YES.
When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain.
maximumZoomScale
:
This value determines how large the content can be scaled. It must be greater than the minimum zoom scale for zooming to be enabled. The default value is 1.0.
Upvotes: 1