我想检测我何时触摸到其他地方,而不是某个特定的位置
UIView
.我在用
touchesBegan
然而,对于它来说,它总是打印“触摸在外面”(见下面的代码)。我错过了什么?
我从这里得到了帮助
post
.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let hitView = self.view.hitTest(touch.location(in: self.view), with: event)
if hitView === checkBackContainer {
print("touch is inside")
} else {
print("touch is outside")
}
}
super.touchesBegan(touches, with: event)
}
里面加了锚
ViewDidLoad
作用
private lazy var checkBackContainer = ImageUploadContainerView()
override func viewDidLoad(){
self.view.addSubview(checkBackContainer)
checkBackContainer.anchorCenterXToSuperview()
checkBackContainer.topAnchor.constraint(equalTo: checkFrontContainer.bottomAnchor, constant: 20).isActive = true
checkBackContainer.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 25).isActive = true
checkBackContainer.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -25).isActive = true
checkBackContainer.heightAnchor.constraint(equalTo: checkBackContainer.widthAnchor, multiplier: 0.42).isActive = true
checkBackContainer.layer.applySketchShadow(color: UIColor.white, alpha: 1.0, x: 0, y: 0.33, blur: 1, spread: 0, cornerRadius: 6)
let backTap = UITapGestureRecognizer(target: self, action: #selector(self.backContainerTapped(_:)))
checkBackContainer.addGestureRecognizer(backTap) }
编辑:
ContainerView是一种习惯
UIView
其中有一些
UIStackView
s
UIlabel
s和
UIImageView
它在里面。我发现这是因为一种习惯
UIView
,当我用常规的
UIView
1.它起作用了。