UITextView 触摸事件未触发

发布于 2024-07-26 14:56:01 字数 1154 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆光下的微笑 2024-08-02 14:56:01

我像这样对 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];
}

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).

@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];
}
扬花落满肩 2024-08-02 14:56:01

更新:我最终在这里使用了该技术:
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

烟花肆意 2024-08-02 14:56:01

您可以通过重写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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文