4

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

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

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

我有一个 IBAction 来检测水龙头并将教程设置为隐藏:

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

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

我如何在水龙头上关闭 UIImageView?

4

4 回答 4

12
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");
    }
}
于 2013-10-22T04:45:55.357 回答
3

您可以像这样制作 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];
  }

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

- (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));
      }
  }
于 2013-10-22T05:11:23.943 回答
2

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

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

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) 给定动作

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

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

可能会有所帮助。

于 2013-10-22T04:51:23.280 回答
0

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

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

把它放在你的 if 语句中。

于 2013-10-22T06:12:08.910 回答