IOS - 如何通过触摸视图之外的任何地方来隐藏视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42385450/
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
IOS - How to hide a view by touching anywhere outside of it
提问by Tester
I'm new to IOS programming, I'm displaying a view when a button is clicked, using the following code inside the button method.
我是 IOS 编程的新手,我在单击按钮时显示一个视图,在按钮方法中使用以下代码。
@IBAction func moreButton(_ sender: Any)
{
self.helpView.isHidden = false
}
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.
最初,self.helpView.isHidden
在viewDidLoad
方法中设置为 true以隐藏视图。现在,我如何通过触摸视图外的任何地方来关闭此视图。从研究中,我发现可以通过创建一个适合整个 viewController 的透明按钮来完成。因此,然后通过单击按钮,我们可以关闭视图。谁能给我 swift 3 中的代码来创建这样的按钮。
Or, if there is any other better way to hide a view, it is welcomed.
或者,如果有其他更好的隐藏视图的方法,欢迎使用。
I'm using Xcode 8.2, swift 3.0
我正在使用 Xcode 8.2,swift 3.0
Thanks in advance.
提前致谢。
回答by Lakshmi Keerthana Siddu
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
}
}
回答by Hanny
In Swift 4
在斯威夫特 4
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if touch?.view == self.view {
commentsTxtView.resignFirstResponder()
}
}
回答by Dharmesh Kheni
You can use touchesBegan
method for that:
您可以touchesBegan
为此使用方法:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.helpView.isHidden = true
}
回答by Deepak Tagadiya
Swift
迅速
override func touchesBegan(_ touches: Set<AnyHashable>, withEvent event: UIEvent) {
var touch: UITouch? = touches.first
if touch?.view != self.yourView {
self.yourView.isHidden = true
}
}
Objective C
目标 C
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if(touch.view != self.yourView){
self.yourView.hidden = YES;
}
}
回答by Santhosh Umapathi
Swift 5.1:
斯威夫特 5.1:
This should help to dismiss the view once touched outside of the view.
一旦在视图之外触摸,这应该有助于消除视图。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
let touch = touches.first
if touch?.view != self.yourView
{ self.dismiss(animated: true, completion: nil) }
}
回答by Clinton D'Souza
Inside the moreButton selected, you can do something like this
在选择的 moreButton 里面,你可以做这样的事情
@IBAction func moreButton(_ sender: Any)
{
self.helpView.isHidden = false
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissView))
view.addGestureRecognizer(tap)
}
func dismissView() {
self.helpView.isHidden = true
self.view.removeGestureRecognizer(tap)
}
回答by Talha Rasool
You can use the below function some this check on the view name will not work but if you have more than one view the problem you will see that if you tap inside your second view the same action will perform. I would prefer to add tapGesture. This is the other solution.
您可以使用下面的函数,一些视图名称的检查将不起作用,但如果您有多个视图,您会发现如果您点击第二个视图内部,则会执行相同的操作。我更愿意添加tapGesture。这是另一种解决方案。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
回答by Frostmourne
in my case I followed it by touchesEnded logic,
就我而言,我遵循了 touchesEnded 逻辑,
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if([touch view]== self.UIViewThatNeedsToBeDismissed) {
NSLog(@"inside view touch");
} else {
NSLog(@"release it");
}
}
回答by mrabins
You could create another transparent button or, your base view (assuming it's a single view below the button) can then address what you're trying to do. First, you would need to make it tappable. Then you want to handle the logic for what happens when tapped, or untapped.
您可以创建另一个透明按钮,或者您的基本视图(假设它是按钮下方的单个视图)然后可以解决您要执行的操作。首先,您需要使其可点击。然后你想处理点击或未点击时发生的逻辑。
回答by josuevhn
You can achieve what you want doing the following (tested with Swift 4.1 and Xcode 9.3):
您可以通过以下方式实现您想要的(使用 Swift 4.1 和 Xcode 9.3 测试):
class ViewController: UIViewController {
...
private var dismissViewTap: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
dismissViewTap = UITapGestureRecognizer(target: self, action: #selector(dismissView))
if let tap = dismissViewTap {
view.addGestureRecognizer(tap)
} // if let
} // viewDidLoad
@objc private func dismissView() {
guard let tap = dismissViewTap else {
return
} // guard
guard helpView.isHidden == false else {
return
} // guard
helpView.isHidden = true
view.removeGestureRecognizer(tap)
} // dismissView
...
} // ViewController
If you want to keep the gesture recognizer (maybe because you open helpView more than once) change dismissView to this version:
如果你想保留手势识别器(可能是因为你打开 helpView 不止一次)改变dismissView 到这个版本:
...
@objc private func dismissView() {
guard helpView.isHidden == false else {
return
} // guard
helpView.isHidden = true
} // dismissView
...
That's all...!
就这样...!