1

UITapGestureRecognizerのサブクラスである Board クラスに を追加してい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の初心者なので、おそらく初心者の間違いです。

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 に答える