xcode 检测用户点击屏幕 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45773879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect that user tap on screen Swift
提问by Jacob233
i want to remove UIVIew
from screen after user tap something except that view. (to visualize it for you I will upload sketch of my view)[
我想UIVIew
在用户点击除该视图之外的其他内容后从屏幕上删除。(为了给你形象化,我会上传我的视图草图)[
]1
] 1
And I want to remove blueUIview
after user tap on something except buttons in this view. What should I use?
我想UIview
在用户点击此视图中除按钮之外的其他内容后删除蓝色。我应该使用什么?
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 addshouldReceiveTouch
function to UIGestureRecognizerDelegate
. What am I doing wrong?
我还将shouldReceiveTouch
功能添加到UIGestureRecognizerDelegate
. 我究竟做错了什么?
回答by yerpy
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
回答by Abdelahad Darwish
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 {
}
}
回答by Turtleeeeee
You can
你可以
- subclass with that background view, and implement an inherited method
-beganTouches:(there is another para but I forget what it is)
- Add a UITapGestureRecognizer to that view
- 具有该背景视图的子类,并实现继承的方法
-beganTouches:(there is another para but I forget what it is)
- 向该视图添加一个 UITapGestureRecognizer
回答by alegelos
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 中