我是
IOS编程的新手,我在单击按钮时显示视图,使用按钮方法中的以下代码.
@IBAction func moreButton(_ sender: Any)
{
self.helpView.isHidden = false
}
最初,在viewDidLoad方法中将self.helpView.isHidden设置为true以隐藏视图.现在,我如何通过触摸视图外的任何位置来关闭此视图.从研究中我发现,可以通过创建适合整个viewController的透明按钮来完成.因此,通过单击按钮,我们可以使视图解散.谁能给我swift 3中的代码来创建这样的按钮.
或者,如果有任何其他更好的方法来隐藏视图,欢迎它.
我正在使用Xcode 8.2,swift 3.0
提前致谢.
解决方法
在触摸开始你应该写得像
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
}
}