我有一个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
事件。
有什么想法吗?
答案 0 :(得分:1)
更新:我最终在这里使用了这个技术: https://devforums.apple.com/message/94569#94569
我不确定这是不是一个bug,但是UITextView确实需要利用触摸事件来进行复制和弹出菜单。粘贴3.0,这样就可以解释为什么它会吞下这个事件。
如果你问我,那就太蹩脚了。
更新:我在这里写了博客:http://benscheirman.com/2009/07/detecting-a-tap-on-a-uitextview
答案 1 :(得分:1)
我将这样的UITextview子类化,即使使用IOS 5.0.1,它似乎也可以工作。关键是要覆盖touchesBegan,而不仅仅是touchesEnded(这是我真正感兴趣的)。
@implementation MyTextView
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesBegan: touches withEvent:event];
else
[super touchesBegan: touches withEvent: event];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(paste:))
return NO;
if (action == @selector(copy:))
return NO;
if (action == @selector(cut:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
return [super canPerformAction:action withSender:sender];
}
- (BOOL)canBecomeFirstResponder {
return NO;
}
- (void)dealloc {
[super dealloc];
}
答案 2 :(得分:0)
您可以通过覆盖canPerformAction:withSender:
方法关闭剪切/复制/粘贴,这样您就可以为所有不想允许的操作返回NO。
请参阅UIResponder documentation ...
希望这会阻止你被吃掉。