UITapGestureRecognizer为location.x值返回0

时间:2012-10-25 03:53:22

标签: objective-c cocoa-touch event-handling

我在我的Board类中添加UITapGestureRecognizer,它是UIView的子类。我想获得Tap事件的位置。这是我的自定义init方法:

- (void)setup
{
    [self setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] 
        initWithTarget:self 
        action:@selector(handleSingleTap:)];
    [self addGestureRecognizer:singleFingerTap];

    ...
}

这是我的singleFingerTap方法:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer 
{
    CGPoint location = [recognizer locationInView:[recognizer.view superview]];
    CGFloat x = location.x;
    NSLog(@"testing = %f", (double)x);
}

这是我在不同位置的几个水龙头的输出:

2012-10-24 22:49:53.077 TicTacToe[15561:f803] testing = 0.000000
2012-10-24 22:49:53.971 TicTacToe[15561:f803] testing = 0.000000
2012-10-24 22:49:54.671 TicTacToe[15561:f803] testing = 0.000000

很明显,我的singleFingerTap方法被调用,但是该位置一直表示X坐标的位置为0。我是一个Objective-C新手,所以这可能是一个新手的错误。

1 个答案:

答案 0 :(得分:1)

这是我修复它的方式:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer 
{
    CGPoint location = [recognizer locationInView:[recognizer.view self]];
    CGFloat x = location.x;
    NSLog(@"testing = %f", (double)x);
}

我需要在我有superview的地方添加自己