iOS-如何通过触摸视图外部的任何位置来隐藏视图 [英] IOS - How to hide a view by touching anywhere outside of it
问题描述
我是IOS编程的新手,单击按钮时会在按钮方法中使用以下代码来显示视图.
I'm new to IOS programming, I'm displaying a view when a button is clicked, using the following code inside the button method.
@IBAction func moreButton(_ sender: Any)
{
self.helpView.isHidden = false
}
最初,
在viewDidLoad
方法中将self.helpView.isHidden
设置为true以隐藏视图.现在,我如何通过触摸视图外部的任何位置来消除该视图.通过研究,我发现可以通过创建适合整个viewController的透明按钮来完成此操作.因此,通过单击按钮,我们可以关闭视图.任何人都可以快速地给我代码3来创建这样的按钮.
initially, the self.helpView.isHidden
is set to true in viewDidLoad
method to hide the view. Now, how can i dismiss this view by touching anywhere outside the view. From the research, i found that, it can be done by creating a transparent button that fits the whole viewController. So then by clicking on the button, we can make the view to dismiss. Can anyone give me the code in swift 3 to create such button.
或者,如果还有其他更好的隐藏视图的方法,那就欢迎它.
Or, if there is any other better way to hide a view, it is welcomed.
我正在使用Xcode 8.2,swift 3.0
I'm using Xcode 8.2, swift 3.0
谢谢.
推荐答案
开始联系时,你应该这样写
In touch began you should write like
override func touchesBegan(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
var touch: UITouch? = touches.first
//location is relative to the current view
// do something with the touched point
if touch?.view != yourView {
yourView.isHidden = true
}
}
这篇关于iOS-如何通过触摸视图外部的任何位置来隐藏视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!