xcode 如何检测视图中任意位置的点击?

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

How do I detect a tap anywhere in the view?

iphoneiosobjective-cxcode

提问by ZuluDeltaNiner

I have a tutorial for my app, which should display only the first time the app is opened and should be tapped to dismiss.

我有我的应用程序的教程,它应该只在应用程序第一次打开时显示,应该点击关闭。

I am initializing a UITapGestureRecognizer in my viewDidLoad:

我正在 viewDidLoad 中初始化一个 UITapGestureRecognizer:

tapper_tut = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapper_tut.cancelsTouchesInView = FALSE;
[self.view addGestureRecognizer:tapper_tut];

and I have an IBAction to detect the tap and set the tutorial to hidden:

我有一个 IBAction 来检测点击并将教程设置为隐藏:

- (IBAction)dismiss_tut{
    if (????????????????) {
        _tutorial.hidden = YES;
    }
}

But I have no idea what to put in the if statement condition, or if this is even that right way to go about this.

但我不知道在 if 语句条件中放什么,或者这是否是解决这个问题的正确方法。

How would I dismiss a UIImageView on a tap?

我如何轻按一下关闭 UIImageView?

回答by Jay Gajjar

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.view];
    if (CGRectContainsPoint(mySensitiveRect, p)) {
        NSLog(@"got a tap in the region i care about");
    } else {
        NSLog(@"got a tap, but not where i need it");
    }
}

回答by Bhavesh

You can make viewDidLoad like this

你可以像这样制作 viewDidLoad

- (void)viewDidLoad 
  { 
      [super viewDidLoad];
      self.view.backgroundColor = [UIColor whiteColor];

      /* Create the Tap Gesture Recognizer */
      self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                   action:@selector(handleTaps:)]; 

     /* The number of fingers that must be on the screen */
      self.tapGestureRecognizer.numberOfTouchesRequired = 1;

     /* The total number of taps to be performed before the gesture is recognized */
      self.tapGestureRecognizer.numberOfTapsRequired = 1;

     /* Add this gesture recognizer to the view */
     [self.view addGestureRecognizer:self.tapGestureRecognizer];
  }

To detect the taps you can make the method like this.

要检测水龙头,您可以使用这样的方法。

- (void) handleTaps:(UITapGestureRecognizer*)paramSender
  {
      NSUInteger touchCounter = 0; 
      for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++)
      {
            CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view];
            NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
      }
  }

回答by Dhaval Bhadania

you have to declare .h file as "UIGestureRecognizerDelegate"

您必须将 .h 文件声明为“UIGestureRecognizerDelegate”

you have getting tap of gesture as two way as given below steps.

您可以通过以下步骤以两种方式点击手势。

1) Call delegate method of GestureRecognizer (not given action )

1) 调用 GestureRecognizer 的委托方法(未给出动作)

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil]; // not given  action.
recognizer.numberOfTouchesRequired=1;// here how many tap you want set it 
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
 {
     //whatever you want write code here
    return NO;
  }

2) given action

2) 给定动作

 UITapGestureRecognizer *oneTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Addphoto)];
 [oneTouch setNumberOfTouchesRequired:1];
 [self.view addGestureRecognizer:oneTouch];

 -(IBAction)Addphoto
 {
     //whatever you want write code here
 }

may be it will help .

可能会有所帮助。

回答by Amulya

I think u need to detect the first time launch of the application which u can do with following

我认为您需要检测应用程序的第一次启动,您可以通过以下方式进行

![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]

![[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]

Put this in your if statement .

把它放在你的 if 语句中。