如何将触摸传递给 UITextView
我有一个 UITextView,它隐藏在另一个名为 canTouchMe 的视图下方。
现在,一旦调用 canTouchMe,我就会将 UITextView 带到前面并将其指定为第一响应者。但是,我希望将光标放置在用户触摸屏幕的任何位置。现在,即使用户触摸了 UITextView 的最开头,光标也会跳转到它的最末尾。
- 有没有办法将触摸传递给 UITextView?
- 或者有没有办法告诉 UITextView 将光标放在 currentPosition 附近?
这是我的代码:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive
if (deltaY == 0 && deltaX == 0) {
NSLog(@"Touch");
[self.view sendSubviewToBack:canTouchMe];
[textView becomeFirstResponder];
}
}
我尝试使用 [textView TouchesBegan:touches withEvent:event] 传递触摸;但这没有用。
I have an UITextView which is hidden beneath another view called canTouchMe.
Now, once canTouchMe is called, I bring UITextView to the front and assign it as the first responder. However, I would like the cursor to be placed wherever the user has touched the screen. Right now, the curser jumps to the very end of the UITextView even if the user touched the very beginning of it.
- Is there a way to pass on the touch to the UITextView?
- Or is there a way to tell the UITextView to put the cursor near the currentPosition?
Here is my code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive
if (deltaY == 0 && deltaX == 0) {
NSLog(@"Touch");
[self.view sendSubviewToBack:canTouchMe];
[textView becomeFirstResponder];
}
}
I tried to pass on the touch with [textView touchesBegan:touches withEvent:event]; but that didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有尝试过这个,但在阅读了 UITextView 我会使用这段代码:
这应该将您滚动到您触摸的位置,并且可能在那里插入光标。让我知道这是否有效。
I haven't tried this but after reading the docs for UITextView I would play with this code:
this should scroll you to the place where you touched and perhaps insert a cursor there. Let me know if that worked.