gpt4 book ai didi

ios - UITapGestureRecognizer 在 UITextView 中触发一次

转载 作者:行者123 更新时间:2023-11-28 22:30:39 26 4
gpt4 key购买 nike

当用户点击 UITextView 时,我需要将 UITextView 中的光标设置在第一个位置。 NSMakeRange 没有帮助我,但我在 How to set cursor position for UITextView on user input? 中找到了解决方案

然后我在我的 UITextView 中添加了 tapGesture。但它只工作一次,第一次,为什么?

textViewMessage.gestureRecognizers = nil;
UITapGestureRecognizer *tapTextView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTextView)];
[textViewMessage addGestureRecognizer:tapTextView];

并且这个方法只调用了一次

- (void)tapTextView
{
[textViewMessage becomeFirstResponder];
}

最佳答案

当 textView 成为第一响应者时,您无法捕捉到它上面的触摸事件。我很快找到了一种 hackish 方法,但我相信你能找到更好的方法。

- (void)tapTextView
{
[textViewMessage setEditable:YES];
[textViewMessage becomeFirstResponder];
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
if ([textView isFirstResponder]) {
[textView setEditable:NO];
}
return YES;
}

您需要将 UITextViewDelegate 添加到头文件中。希望对您有所帮助。

关于ios - UITapGestureRecognizer 在 UITextView 中触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com