4

我有一个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事件。

有任何想法吗?

4

3 回答 3

1

更新:我最终在这里使用了该技术: https ://devforums.apple.com/message/94569#94569

我不确定这是否是一个错误,但 UITextView 确实需要利用触摸事件来执行 3.0 的复制和粘贴弹出菜单,所以这可以解释为什么它会吞下这个事件。

如果你问我,那就太蹩脚了。

更新:我在这里写了一篇博客:http: //benscheirman.com/2009/07/detecting-a-tap-on-a-uitextview

于 2009-07-08T19:22:07.930 回答
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];
}
于 2012-01-05T22:13:34.527 回答
0

您可以通过覆盖该方法来关闭剪切/复制/粘贴canPerformAction:withSender:,因此您可以为所有您不想允许的操作返回 NO。

请参阅UIResponder 文档...

希望这会阻止你的触摸被吃掉。

于 2009-07-08T12:59:55.980 回答