ios - 在重叠区域点击手势事件

标签 ios swift uiview uitapgesturerecognizer

我有一个带有黑色边框的 View ,它有两个不同的 View 。而且这些观点在一个小范围内是重叠的。他们每个人都有自己的 UITapGestureRecognizer。当我点击每个项目的离散区域时,会触发该项目的操作。但是当我点击公共(public)区域时,只会触发第二个 View 的操作。我希望必须触发这两个 Action 。我怎样才能做到这一点?这是我的代码:

class ViewController: UIViewController {

  @IBOutlet weak var view1: UIView!
  @IBOutlet weak var view2: UIView!
  @IBOutlet weak var outerView: UIView!

  override func viewDidLoad() {
      super.viewDidLoad()
      outerView.layer.borderWidth = 2.0
      outerView.layer.borderColor = UIColor.black.cgColor
      view1.layer.borderWidth = 2.0
      view1.layer.borderColor = UIColor.red.cgColor
      view2.layer.borderWidth = 2.0
      view2.layer.borderColor = UIColor.blue.cgColor
      self.initialize()
  }

  private func initialize(){
      let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(detectTap1(_:)))
      let tapGesture2 = UITapGestureRecognizer(target: self, action: #selector(detectTap2(_:)))
      self.view1.addGestureRecognizer(tapGesture1)
      self.view2.addGestureRecognizer(tapGesture2)
  }

  @objc func detectTap1(_ gesture : UITapGestureRecognizer) {
      print("detectTap1")
  }

  @objc func detectTap2(_ gesture : UITapGestureRecognizer) {
      print("detectTap2")
  }
}

example

请分享您的建议。

最佳答案

对于这个问题,我找到了这个解决方案,也许不是最好的解决方案,但它有效,无论如何我会寻求进一步的改进

我已经子类化了 UIGestureRecognizer

已更新

import UIKit
import UIKit.UIGestureRecognizerSubclass

class CustomGestureRecognizer: UIGestureRecognizer {

    var anotherGestureRecognizer : CustomGestureRecognizer?
    private var touchBeganSended : Bool = false
    private var touchLocation : CGPoint?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesBegan(touches, with: event)
        if let validTouch = touches.first?.location(in: self.view) {
            if (self.view!.point(inside: validTouch, with: event)) {
                if(!touchBeganSended) {
                    touchBeganSended = true
                    touchLocation = validTouch
                    anotherGestureRecognizer?.touchesBegan(touches, with: event)
                    state = .recognized
                }
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        super.touchesEnded(touches, with: event)
        if let validTouch = touches.first?.location(in: self.view) {
            if (self.view!.point(inside: validTouch, with: event)) {
                if(touchBeganSended) {
                    touchBeganSended = false
                    anotherGestureRecognizer?.touchesEnded(touches, with: event)
                    state = .recognized
                }
            }
        }
    }

    override func location(in view: UIView?) -> CGPoint {
        if let desiredView = view {
            if(desiredView == self.view) {
                return touchLocation ?? CGPoint(x: 0, y: 0)
            } else {
                return super.location(in: view)
            }
        } else {
            return super.location(in: view)
        }
    }
}

已更新

那么你需要将你的initialize()方法修改为这个方法,最后一个update 你不需要考虑哪个 View 在 View 层次结构的顶部

private func initialize(){
    let tapGesture1 = CustomGestureRecognizer(target: self, action: #selector(detectTap1(_:)))
    let tapGesture2 = CustomGestureRecognizer(target: self, action: #selector(detectTap2(_:)))
    tapGesture1.cancelsTouchesInView = true
    tapGesture1.delegate = self
    tapGesture2.cancelsTouchesInView = true
    tapGesture2.delegate = self
    self.view1.addGestureRecognizer(tapGesture1)
    tapGesture1.anotherGestureRecognizer = tapGesture2
    tapGesture2.anotherGestureRecognizer = tapGesture1
    self.view2.addGestureRecognizer(tapGesture2)
}

正如你在这里看到的那样有效

enter image description here

关于ios - 在重叠区域点击手势事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58129160/

相关文章:

ios - 当 ViewController 可能实现或不实现协议(protocol)时,在 UIViewController 的 viewDidLoad 上调用协议(protocol)函数

ios - 如何解决 `No method with selector is implemented in this translation unit` ?

ios - 给 UITextView 一个可点击的 URL 链接

ios - Xcode 11 GM Seed 2 在构建工作区/项目时卡住

ios - 如何在向左或向右移动时水平翻转 Sprite ?

ios - UIView 不记得它的标签

ios - 在 ViewController 中创建选项卡 View 的最佳方法是什么

ios - Xcode 构建成功但 iOS 模拟器没有运行

ios - 长按 EXC_BAD_ACCESS;没有僵尸事件

ios - 如何在用户放大 UIScrollView 时隐藏某些内容,然后在他们缩小时显示它?