从重叠视图触发 UITapGestureRecognizers [英] Triggering UITapGestureRecognizers from overlapping Views

查看:24
本文介绍了从重叠视图触发 UITapGestureRecognizers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主视图和 4 个主视图的子视图,它们都有自己的 UITapGestureRecognizer,当我点击一个子视图时,如何触发这两个视图.下面的例子,

I have one main view and 4 subviews of the mainview they all have their UITapGestureRecognizer, when I tap on one subview how can it be triggered both views. Example below,

如果我点击子查看 1 个想要的日志:

if I tap to subview 1 desired log would be:

subview1 is clicked
MainView is clicked

我的代码

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let mainGesture = UITapGestureRecognizer(target: self, action: #selector(mainGestureActivated))

    self.view.addGestureRecognizer(mainGesture)

    let subGesture = UITapGestureRecognizer(target: self, action: #selector(subViewGestureActivated))

    self.subview1.addGestureRecognizer(subGesture)


}

@objc func mainGestureActivated(){
    print("MainView Clicked")
}

@objc func subViewGestureActivated(){
    print("Subview Clicked")
}

它只打印点击的子视图!是否可以同时触发两个gestureRecognizers,因为main 正在封装其他.

it prints only subview clicked! Is it possible to trigger both gestureRecognizers since main is encapsulating other.

推荐答案

首先你应该符合你的VC中的UIGestureRecognizerDelegate,然后实现shouldRecognizeSimultaneouslyWith的delegate func.在函数内部,你应该检测gestureRecognizerotherGestureRecognizer是否是你想要的,如果是,你应该允许它们同时工作,

First you should conform to UIGestureRecognizerDelegate in your VC, and then implement the delegate func of shouldRecognizeSimultaneouslyWith. Inside the function, you should detect if the gestureRecognizer, and the otherGestureRecognizer are the wants you want, and if so, you should allow them to work simultaneously,

  • 符合委托,并在 viewDidLoad 之外声明手势识别器(因为您稍后需要在委托方法中访问它们.)

  • Conform to delegate, and Declare gesture recognizers outside of viewDidLoad (because you need to access them in the delegate method later.)

var mainGestureRecognizer = UITapGestureRecognizer()var subGestureRecognizer = UITapGestureRecognizer()

初始化您的识别器,并将您的 VC 设置为其委托:

Initialize your recognizers, and set your VC as their delegate:

mainGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(mainGestureActivated))
subGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(subViewGestureActivated))
mainGestureRecognizer.delegate = self
subGestureRecognizer.delegate = self

  • 实现上面提到的delegate函数,可以同时识别subView和mainView:

  • Implement the delegate function mentioned above to allow simultaneous recognition for subView and mainView:

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == subGestureRecognizer && otherGestureRecognizer == mainGestureRecognizer {
            return true
        }
    
        return false
    }
    

  • 如果您希望它适用于 4 个不同的子视图,那么您应该检查委托方法中的 else if 语句.

    And if you want it to work for 4 different subviews, then you should check with else if statements inside the delegate method.

    这篇关于从重叠视图触发 UITapGestureRecognizers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆