My Setup
I'm trying to replicate Google Now's card interface.
I have a UIView
which is a subview of (inside) a UIScrollView
. Both are controlled from the same UIViewController
. The UIView
has a UIPanGestureRecognizer
attached to it by the UIViewController
.
The UIScrollView
is used to scroll up and down. The UIPanGestureRecognizer
is used to swipe away the UIView
.
The Problem
I can't scroll when I'm touching the UIView
(the card)
The Question
How do I implement UIGestureRecognizerDelegate
to enable gestureRecognizer(_:shouldRecognizeSimultaneouslyWithGestureRecognizer:)
on the card so that it'll let the UIScrollView
scroll?
I've spent several hours trying to figure this out, and I would be incredibly thankful for help.
You can select right, left, up or down. One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture and downGesture), change the connection to: Action and type to: UISwipeGestureRecognizer.
A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.
Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.
In case anyone is wondering how to do this, here's the answer:
When you declare your class, add UIGestureRecognizerDelegate
after the class it subclasses. Here's what that looks like in my case:
class CardViewController: UIViewController, UIGestureRecognizerDelegate
Next, you add this function to the body of the UIViewController
:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
And finally, you do this to the UIPanGestureRecognizer
:
somePanGestureRecognizer.delegate = self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With