1

我正在向UITapGestureRecognizer我的 Board 类添加一个,它是UIView. 我想获取 Tap 事件的位置。这是我的自定义初始化方法:

- (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 新手,所以这可能是一个新手错误。

4

1 回答 1

1

这就是我修复它的方法:

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

我需要在我有超级视图的地方添加自我

于 2012-10-25T16:09:15.433 回答