ios 如何让 UITextView 检测一键点击?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
//test
UITouch *touch = [event allTouches] anyObject];
if ([touch tapCount] == 2) {
NSLog (@"tapcount 2");
[self.textview becomeFirstResponder];
}
else if ([touch tapCount] == 1) {
NSLog (@"tapcount 1");
[self.textview becomeFirstResponder];
[self.view performSelector:@selector(select:)];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(@"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"****touchesEnded");
[self.nextResponder touchesEnded: touches withEvent:event];
NSLog(@"****touchesEnded");
[super touchesEnded:touches withEvent:event];
NSLog(@"****touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
NSLog(@"touchesCancelled");
}
我的问题:
我想在 UITextView(此代码中的 textview)上点击一次时模拟两次点击。但是,当我在 textview 上点击一次或两次时,我不会从一次或两次点击中获取 NSLog,仅在其外部。我应该怎么做才能让它发挥作用?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan");
//test
UITouch *touch = [event allTouches] anyObject];
if ([touch tapCount] == 2) {
NSLog (@"tapcount 2");
[self.textview becomeFirstResponder];
}
else if ([touch tapCount] == 1) {
NSLog (@"tapcount 1");
[self.textview becomeFirstResponder];
[self.view performSelector:@selector(select:)];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(@"touchesMoved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"****touchesEnded");
[self.nextResponder touchesEnded: touches withEvent:event];
NSLog(@"****touchesEnded");
[super touchesEnded:touches withEvent:event];
NSLog(@"****touchesEnded");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesCancelled:touches withEvent:event];
NSLog(@"touchesCancelled");
}
MY QUESTION:
I want to simulate two taps when tapping once on a UITextView
, which is textview in this code. But I do not get NSLog from one and two taps when I tap either once or twice on textview, only outside it. What should I do to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我会使用两个 手势识别器 这里。
选择器就像:
Probably I would use two gesture recognizers here.
And the selectors would just be like:
我遇到了类似的问题,我想捕获 textView 上的点击而不编辑底层文本。
此答案适用于 Swift 3.0。
对于我的解决方案,我实现了 textView 委托方法 textViewShouldBeginEditing 并返回 false 值。这使我能够捕获 textView 上的点击,而无需任何额外的开销。
只需确保指定 textView 来使用 ViewController 类中的委托即可。
I encountered a similar issue where I wanted to capture a tap on a textView without editing the underlying text.
This answer is for Swift 3.0.
For my solution, I implemented the textView delegate method textViewShouldBeginEditing and returned false for the value. This allows me to capture taps on the textView without any additional overhead.
Just be sure to assign the textView to use the delegate in your ViewController class.
我遇到了同样的问题,其他答案对我不起作用。这就是我所做的。
我按照另一个答案的建议附上了手势。然后我确保调用了选择的委托方法。我确实尝试简单地选择单元格,但这并没有触发委托方法。我相信只有用户交互才会导致调用委托方法,因此此代码模仿了该行为。
https://gist.github.com/brennanMKE/e89bf7a28d96812d6a22
I had the same problem and the other answers did not work for me. So this is what I did.
I attached the gesture like another answer suggested. Then I made sure the delegate method for selection was called. I did try simply selecting the cell but that did not fire the delegate method. I believe that only user interactions cause the delegate method to be called, so this code mimics that behavior.
https://gist.github.com/brennanMKE/e89bf7a28d96812d6a22