如何在UITextView上获取tap的坐标

时间:2013-07-01 20:49:34

标签: objective-c uitextview coordinates

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

- (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);
}

1 个答案:

答案 0 :(得分: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);
}