[英]Detect that user tap on screen Swift
i want to remove UIVIew
from screen after user tap something except that view.我想在用户点击除该视图之外的其他内容后从屏幕中删除
UIVIew
。 (to visualize it for you I will upload sketch of my view)[ (为了给你形象化,我将上传我的视图草图)[
And I want to remove blue UIview
after user tap on something except buttons in this view.并且我想在用户点击此视图中除按钮之外的其他
UIview
后删除蓝色UIview
。 What should I use?我应该使用什么?
EDIT: In blue UIVIew
are two buttons and I want to remove that view when user tap on background image编辑:在蓝色
UIVIew
有两个按钮,我想在用户点击背景图像时删除该视图
I did what "yerpy" told me to do but it isn't working.我做了“yerpy”告诉我要做的事情,但它不起作用。
func test(gestureRecognizer: UITapGestureRecognizer) {
print("test")
}
func setUpBackgroundImageView() {
self.view.addSubview(backgroundImageView)
backgroundImageView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
backgroundImageView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
backgroundImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
backgroundImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let tap = UITapGestureRecognizer(target: self, action: #selector(test(gestureRecognizer:)))
backgroundImageView.addGestureRecognizer(tap)
tap.delegate = self
}
And I also add shouldReceiveTouch
function to UIGestureRecognizerDelegate
.我还将
shouldReceiveTouch
函数添加到UIGestureRecognizerDelegate
。 What am I doing wrong?我做错了什么?
Add UIGestureRecognizer
to the super view
:将
UIGestureRecognizer
添加到super view
:
As you said you have image view as a background.正如你所说,你有图像视图作为背景。
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped(gestureRecognizer:)))
imageView.addGestureRecognizer(tapRecognizer)
tapRecognizer.delegate = self
Adding target action :添加目标操作:
func tapped(gestureRecognizer: UITapGestureRecognizer) {
// Remove the blue view.
}
And then inside UITapGestureRecognizerDelegate
:然后在
UITapGestureRecognizerDelegate
里面:
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if touch.view!.superview!.superclass! .isSubclass(of: UIButton.self) {
return false
}
return true
}
}
Hope it helps !希望它有帮助!
EDIT编辑
Make sure that user can touch on the view
by enabling : self.view.userInteractionEnabled = true
通过启用确保用户可以触摸
view
: self.view.userInteractionEnabled = true
you can use UITapGestureRecognizer for that view您可以为该视图使用 UITapGestureRecognizer
override func viewDidLoad() {
super.viewDidLoad()
// Add "tap" press gesture recognizer
let tap = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
tap.numberOfTapsRequired = 1
backgroundimage.addGestureRecognizer(tap)
}
// called by gesture recognizer
func tapHandler(gesture: UITapGestureRecognizer) {
// handle touch down and touch up events separately
if gesture.state == .began {
} else if gesture.state == .ended {
}
}
You can你可以
-beganTouches:(there is another para but I forget what it is)
-beganTouches:(there is another para but I forget what it is)
1- Add a view below your view, let call it overlay (gray one) 1- 在您的视图下方添加一个视图,称之为叠加(灰色)
2- Add your container view with all your buttons inside (green one) 2- 添加包含所有按钮的容器视图(绿色按钮)
3- Add a tap gesture to the overlay (drag tap to overlay view) 3- 向叠加层添加点击手势(拖动点击以叠加视图)
4- Create a @IBAction of the tap to the viewcontroller 4- 为视图控制器创建一个@IBAction 的点击
5- Write code to hide your green view inside the @IBAction 5- 编写代码将您的绿色视图隐藏在@IBAction 中
Add a giant button that covers the entire screen under all the buttons.在所有按钮下添加一个覆盖整个屏幕的巨大按钮。 Anytime the user presses on the giant button underneath the smaller buttons, do what you want it to do.
每当用户按下较小按钮下方的巨大按钮时,就可以执行您希望它执行的操作。
Not the most elegant but it works :P不是最优雅的,但它有效:P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.