如何获取UItextview触摸坐标位置

发布于 2024-10-21 23:25:45 字数 1264 浏览 5 评论 0原文

我需要通过触摸视图来获取坐标位置。我触摸的视图是UITextview。视图的层次结构是 UIViewController -> UITextview。我想我可以使用 locationInView 方法来获取该坐标。我知道 UITextview 有一些委托方法,所以我想我必须使用 textViewShouldBeginEditing 委托方法来调用 locatioInView。

这是我的代码片段,

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {

  NSSet *touches;
  UITouch *touch = [touches anyObject];

  CGPoint currentTouchPosition = [touch locationInView:textView];
  NSLog(@"currentTouchPosition: x=%f, y=%f", currentTouchPosition.x, currentTouchPosition.y);

  return YES;
}

我的问题是,currentTouchPosition 在 x 和 y 中始终具有 0 值。我不知道该代码有什么问题。 有人可以告诉我吗???

谢谢

更新 最后我解决了这个问题。我在 viewDidLoad 中使用了 UITapGestureRecognizer,然后使用我在 textViewShouldBeginEditing 中使用的相同方法获取点击位置。这是我编写的代码:

- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
tap.delegate=self;
[self.textView addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;

[tap release];
}

这是handleTapFrom:

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {

locationTap = [recognizer locationInView:self.textView];

}

i need to get the coordinate position from touching a view. The view i touch is UITextview. THe hierarchy of the view is UIViewController -> UITextview. I think i can use locationInView method to get that coordinate. I know that UITextview have some delegate methods, so i think i have to use textViewShouldBeginEditing delegate method to call the locatioInView.

This is my code snippet

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {

  NSSet *touches;
  UITouch *touch = [touches anyObject];

  CGPoint currentTouchPosition = [touch locationInView:textView];
  NSLog(@"currentTouchPosition: x=%f, y=%f", currentTouchPosition.x, currentTouchPosition.y);

  return YES;
}

my problem is, the currentTouchPosition always have 0 value in x and y. I dont know what's wrong with that code.
Can somebody tell me???

thank you

UPDATE
Finally i've solved this problem. I have use UITapGestureRecognizer in viewDidLoad and then get the tap location using the same method i was use in textViewShouldBeginEditing. This is the code i've made :

- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
tap.delegate=self;
[self.textView addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;

[tap release];
}

and this is the handleTapFrom :

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {

locationTap = [recognizer locationInView:self.textView];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-10-28 23:25:46

您只需声明一个 NSSet 对象并从中检索数据,该数据始终为空。
取一个 UITextView 的子类并在其中写下 touch begin 方法,即 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event。参数 Touches 将为您提供正确的触摸指向视图。使用您的子类对象作为您的 TextView。

Your simply declaring one NSSet object and retrieving data from it which always gives eampty.
Take a subClass of UITextView and write down touch begin method in it i.e -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event.The parameter touches will then give you the correct touch point on the view.Use your subclass object as your TextView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文