0

我正在使用这种方法,但它仅适用于我的 UIView 子类。如果我点击 UITextView 或 UIButton 或任何其他对象,它不会显示坐标。如何获取 UITextView 上的点击坐标?为什么会这样?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *userTouch = [[event allTouches] anyObject];
    CGPoint currentPos = [userTouch locationInView:self.view];
    NSLog(@"Coordinates: (%f,%f)", currentPos.x, currentPos.y);
}
4

1 回答 1

0

在 ViewDidLoad 方法中添加此代码,

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)];
singleTap.numberOfTapsRequired = 1;
[self.textView addGestureRecognizer:singleTap];

实现上述方法

-(void)singleTapRecognized:(id)sender{
    CGPoint tapPoint = [sender locationInView:self.textView];
    NSLog(@"Coordinates: (%f,%f)", tapPoint.x, tapPoint.y);
}
于 2013-07-02T04:32:42.083 回答