0

Just started iOS programming, trying to get UITapGestureRecognizer working using iOS 5.1 simulator, and i can't seem to get the right values when tapping. here's my code that i took from nathan eror's answer to nathan eror's How to add a touch event to a UIView?

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    CGPoint location = [recognizer locationInView:[recognizer.view superview]];

    NSLog(@"touched points: %g, %g", location.x, location.y);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor blackColor];
    NSLog(@"gets in here");
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];
}

and i keep getting 0.000000 for my values every time i tap on the trackpad on my macbook pro on the simulator. is it because of the simulator? or am i doig something morbidly wrong?

P.S does anyone know how to highlight the code as objective-C in posting this question? thnx in advance

Community
  • 1
  • 1
David T.
  • 22,301
  • 23
  • 71
  • 123
  • When you say "tap on the trackpad", do you actually mean _click_, or really _tap_? A click on the simulator window gets treated as a tap inside the simulator. Taps on the physical trackpad have no significance to the simulator -- they don't get passed on, AFAIK. – jscs Aug 08 '12 at 05:49
  • @DavidT The site will automatically highlight your code as Objective-C if your question is tagged Objective-C. However, the editor preview usually doesn't highlight in real-time. – rob mayoff Aug 08 '12 at 05:52
  • @W'rkncacnter gotcha. umm... i guess more like "click" on the trackpad. so it was simulating the "tap" on simulator – David T. Aug 08 '12 at 06:57
  • @robmayoff gotcha, thanks, and no wonder. – David T. Aug 08 '12 at 06:57

2 Answers2

2

I can reproduce your problem. It appears to happen because [recognizer.view superview] is the window (class UIWindow). Although UIWindow is a subclass of UIView, the gesture recognizer doesn't seem to convert the location to the window's coordinate system correctly. If I add a subview to self.view and put the gesture recognizer on that subview, it converts the point to self.view's coordinate system correctly.

I suggest you file a bug report at http://bugreport.apple.com. (The product is “iPhone SDK”.)

If you really need to get the point in the window's coordinate system, you can do this:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    CGPoint location = [recognizer locationInView:recognizer.view];
    location = [recognizer.view convertPoint:location toView:recognizer.view.superview];

    NSLog(@"touched point: %g, %g", location.x, location.y);
}

However, the window's coordinate system includes space for the status bar, and doesn't rotate when you rotate the device. It's probably not what you want.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

Try NSLog(@"touched point %@", NSStringFromCGPoint(location))

Bernd Rabe
  • 790
  • 6
  • 23