UITextView 触摸事件未触发
我有一个 UITextView
,我想检测一次点击。
看起来我只需覆盖 touchesEnded:withEvent
并检查 [[touches anyObject] tapCount] == 1
就可以了,但是这个事件甚至不会触发。
如果我像这样重写 4 个事件:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesBegan (tapCount:%d)", touch.tapCount);
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesEnded (tapCount:%d)", touch.tapCount);
[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches cancelled");
}
我得到这样的输出:
> touchesBegan (tapCount:1)
> touchesCancelled
> touchesBegan (tapCount:1)
> touches moved
> touches moved
> touches moved
> touchesCancelled
看来我从来没有得到 touchesEnded
事件。
有任何想法吗?
I have a UITextView
that I want to detect a single tap for.
It looks like I would be fine with simply overriding touchesEnded:withEvent
and checking [[touches anyObject] tapCount] == 1
, however this event doesn't even fire.
If I override the 4 events like this:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesBegan (tapCount:%d)", touch.tapCount);
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches moved");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSLog(@"touchesEnded (tapCount:%d)", touch.tapCount);
[super touchesEnded:touches withEvent:event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches cancelled");
}
I get output like this:
> touchesBegan (tapCount:1)
> touchesCancelled
> touchesBegan (tapCount:1)
> touches moved
> touches moved
> touches moved
> touchesCancelled
It seems I never get the touchesEnded
event.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我像这样对 UITextview 进行子类化,即使在 IOS 5.0.1 中,这似乎也能工作。 关键是还要覆盖touchesBegan,而不仅仅是touchesEnded(这是我真正感兴趣的)。
I subclassed UITextview like so, which seems to work, even with IOS 5.0.1. The key is to override touchesBegan as well, not just touchesEnded (which is what I'm really interested in).
更新:我最终在这里使用了该技术:
https://devforums.apple.com/message/94569#94569
我是不确定这是否是一个错误,但 UITextView 确实需要利用触摸事件来执行复制和弹出菜单。 3.0 的粘贴,这样可以解释为什么它吞掉这个事件。
如果你问我的话,那真是太蹩脚了。
更新:我在这里写了一篇博客:http://benscheirman。 com/2009/07/检测-a-tap-on-a-uitextview
Update: I ended up using the technique here:
https://devforums.apple.com/message/94569#94569
I'm not sure if this is a bug or not, but the UITextView does need to utilize the touch events to do the popup menu for copy & paste for 3.0, so that might explain why it swallows this event.
Pretty lame if you ask me.
Update: I blogged about this here: http://benscheirman.com/2009/07/detecting-a-tap-on-a-uitextview
您可以通过重写canPerformAction:withSender:方法来关闭剪切/复制/粘贴,这样您就可以对所有您不想允许的操作返回NO。
请参阅 UIResponder 文档...
希望这能阻止您的触摸被吃掉。
You can turn off Cut/Copy/Paste by overriding the
canPerformAction:withSender:
method, so you could just return NO for all the actions you don't want to permit.See the UIResponder documentation...
Hopefully that will stop your touches from being eaten.