如何将触摸传递给 UITextView

发布于 2024-11-02 18:42:37 字数 1015 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

怼怹恏 2024-11-09 18:42:37

我还没有尝试过这个,但在阅读了 UITextView 我会使用这段代码:

[myUITextView scrollRangeToVisible:[myUITextView selectedRange]];

这应该将您滚动到您触摸的位置,并且可能在那里插入光标。让我知道这是否有效。

I haven't tried this but after reading the docs for UITextView I would play with this code:

[myUITextView scrollRangeToVisible:[myUITextView selectedRange]];

this should scroll you to the place where you touched and perhaps insert a cursor there. Let me know if that worked.

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