gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:01 27 4
gpt4 key购买 nike

我有一个带有黑色边框的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com