2012-10-25 75 views
1

我將UITapGestureRecognizer添加到我的Board類,該類是UIView的子類。我想獲取Tap事件的位置。這裏是我的自定義init方法:UITapGestureRecognizer對於location.x返回0值

- (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

這是我的固定它:

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

我需要添加自己在那裏我有上海華