How to use UIGestureRecognizerDelegate?

China☆狼群 提交于 2019-12-21 15:21:05

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/31082866/how-to-use-uigesturerecognizerdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!