I have two UIViews (custom scroll views) that respond to UISwipeGestureRecognizer. These two UIViews are subviews of the parent UIView. I want to allow only one of the subviews to respond to the recognizer.
In other words, only one should be allowed to be swiped at a time.
Setting setExclusiveTouch = YES on the subviews AND/OR the parent view doesn't have any effect.
How can I make sure that only 1 subview is being swiped at a time?
Here's a picture:
UIScrollView's by default have multipleTouchEnabled set to YES - if you do not need multiple touch (zooming for example requires it), then set it to NO.
Related
Suppose there are two UIVIew subclasses in two different regions of the screen.
The first UIView subclass is a table or collectionView
The second view is a simple UIView.
Is it possible for gestures inside the second view to be "carried over" to the first view so that for example, a swipe up gesture in the second view would make the table/collection view scroll up ?
In WWDC 2012 Session 223: Enhancing User Experience with Scroll Views, Apple engineers explained that you can take a scroll view's panGestureRecognizer and add it to a different view to make that other view control the scroll view's scrolling.
Note that UITableView and UICollectionView are subclasses of UIScrollView.
So if you can put your table view and your second view into a common superview, you can move the table view's panGestureRecognizer to the superview and it will detect touches on both subviews.
[commonSuperview addGestureRecognizer:tableView.panGestureRecognizer];
The best way to achieve that is to encapsulate your 2 views in a another view and to add the GestureRecognizer on the motherView.
This way when the gesture triggers, with the location of the touch you can figure out if it started in one view or the other and you can track the move all the way to the second view.
I have a UIScrollView and a subview of that view is a UISlider. I am finding that when I try to slide the UISlider the UIScrollView scrolls.
Can I make it so the scroll view only slides if I am not touching the UISlider?
You could use this self.slider.exclusiveTouch = YES;
From the Apple Documentation:
Restrict event delivery to a single view. By default, a view’s exclusiveTouch property is set to NO, which means that one view does not block other views in a window from receiving touches. If you set the property to YES for a specific view, then that view receives touches if—and only if—it is the only view tracking touches.
So by setting your slider's exclusiveTouch property, you can prevent scrollView from receiving the touches and therefore it will not slide.
I have a scrollView and it contains a subView. SubView has a panGesture added to it.
What I want:
I need pangesture to work on subview only if the panning is done vertically. If the panning happens horizontally, I want the scrollView to respond for the panning instead of subview. How can I acheive this
Note: Iam able to find programatically when the panning happens horizontally.
There are several ways to do this:
Depending on what your subview does, you maybe able to replace it with a scrollview and lock it to scroll only vertically. (while the super should scroll only horizontally).
You can try feeding the touch information from the subview to it's superview if the pan gesture is moving horizontally. You could do this using a delegate pattern or, if you know what you're superview is, just access superview in your subview. However, this creates a strong coupling between the two views and probably shouldn't be done. It would be fragile and many would consider it code smell.
(best option) - Subclass UIGestureRecognizer and create your own gesture recognizer that only recognizes vertical movement. Use that instead of the UIPanGestureRecognizer.
Option 3 is the best, but probably the most work. You'll want to read Apple's docs on subclassing UIGestureRecognizer: Apple Docs - UIGestureRecognizer.
I've also written my own drag gesture recognizer, FlxDragGestureRecognizer.m, FlxDragGestureRecognizer.h (similar to a pan gesture recognizer), which you can use if you'd like, or take a look at to get a good idea of how to subclass UIGestureRecognizer. You can use this class to recognize touches moving only in certain directions, otherwise it will fail (which will allow other gesture recognizer to recognizer, like the scrollview). It also has a lot of other customizations and information it gathers, like velocity.
Since you already know how to detect the panning, then in you will need to implement a delegate protocol in your superview, a delegate property in your subview and assign your superview as a delegate for your subview.
when detecting horizontal panning, call the delegate method, when detecting vertical panning, implement what you want in the subview.
I have a small view (View Frame Size : 100,0,20,30), i want to add two gesture recognizer one is UIPanGestureRecognizer and another one is my own custom GesuterRecognizer "CPPinGestureRecognizer".
UIPanGestureRecognizer is used for drag the view in horizontally.
Example: the dragging bounds are :(100,0 to 320, 0)
CPPinGestureRecgonizer is used for enlarge the view in vertically with the same width.
Example: (enlarge view height up to user drag the view)
Now the problem is both gesture are working together and i want to check if the view dragged horizontally means want to fail CPPinGestureRecognizer, if the user dragged vertically means want to fail UIPanGestureRecognizer.
how can i achieve this.
Thanks in advance.
Gesture recognizers can have a delegate (UIGestureRecognizerDelegate), and this protocol has 2 methods: gestureRecognizer:shouldRequireFailureOfGestureRecognizer: and gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer. You can use those to not allow one to be recognized while the other one is currently being recognized.
I have a horizontal, paged scroll view that houses multiple view controllers, each of which contains a UIImageView. Within each view controller, I am adding a Tap GestureRecognizer to the ImageView so I can perform a specific action when the image is tapped/double tapped.
However, the ImageView gesture recgonizers don't seem to be firing, and therefore the selectors are not getting called.
I anticipate it may have something to do with the Imageview's being housed in the scrollview, but I am not sure where to start. Is there anything specific I need to do to ensure that the gesture recognizer gets called for each imageview within the scrollview?
Thanks for your suggestions.
Perhaps your UIImageView is not configured to receive user interaction
UIImageView has in default
userInteractionEnabled set to NO. I
would try to change it to YES.
UIgestureRecognizer in a view inside a UIScrollView