使用单击手势隐藏/取消隐藏 Xcode 中的按钮或插座

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10374970/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:08:14  来源:igfitidea点击:

hide/unhide button or outlets in Xcode using a single tap gesture

xcodehideshowuitapgesturerecognizer

提问by redribbon

this is my first time asking for some help here although I've been lurking on this forum for the past 6 months. so here a simple one and I know its been asked like many times before but none of them comes with a simple answer that really helped. hope that maybe someone here who kindly enough can help me and many others that need this with a helpful tips.

这是我第一次在这里寻求帮助,尽管过去 6 个月我一直潜伏在这个论坛上。所以这里是一个简单的问题,我知道它之前被问过很多次,但没有一个给出真正有帮助的简单答案。希望这里的某个足够好心的人可以通过有用的提示帮助我和许多其他需要它的人。

so this is what I already did to hide the outlets :

所以这就是我已经为隐藏网点所做的:

in the header file :

在头文件中:

@interface tapgestureViewController : UIViewController {

IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
IBOutlet UITextView *text;

}

-(IBAction)hideOutlets;

@end

and on the implementation file :

并在实现文件上:

-(IBAction)hideOutlets:(UITapGestureRecognizer*)singleTap {

btn1.hidden = YES;
btn2.hidden = YES;
text.hidden = YES;

}

my simple question is : how to unhide/show the outlets again if the user tap again on the screen?

我的简单问题是:如果用户再次点击屏幕,如何再次取消隐藏/显示插座?

回答by Majster

I think that the simplest way to accomplish this is by using:

我认为最简单的方法是使用:

-(IBAction)hideOutlets:(UITapGestureRecognizer*)singleTap 
{
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    text.hidden = !text.hidden;
}

This will simply negate your current bool state and you're done :)

这将简单地否定您当前的 bool 状态,您就完成了 :)

EDIT: To check if the touch was somewhere on the form but not on the buttons or text field try using this. It should work. No need to use the UITapGestureRecognizeranymore.

编辑:要检查触摸是否在表单上的某处而不是在按钮或文本字段上,请尝试使用它。它应该工作。没必要再用UITapGestureRecognizer了。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint loc = [touch locationInView:[touch view]];
    if (!(CGRectContainsPoint(btn1.frame, loc) || CGRectContainsPoint(btn2.frame, loc) || CGRectContainsPoint(text.frame, loc)))
    {
        btn1.hidden = !btn1.hidden;
        btn2.hidden = !btn2.hidden;
        text.hidden = !text.hidden;
    }
}