Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UIGestureRecognizerDelegate?

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.

like image 860
DefinitelyNotAPlesiosaur Avatar asked Jun 26 '15 22:06

DefinitelyNotAPlesiosaur


People also ask

How do I use UISwipeGestureRecognizer?

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.

What is Pan gesture in IOS?

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.

How do I add tap gestures in swift 5?

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.


1 Answers

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

like image 61
DefinitelyNotAPlesiosaur Avatar answered Oct 28 '22 09:10

DefinitelyNotAPlesiosaur