2009-07-08 186 views
4

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

任何想法?

+0

如果你把你的电话转到超级会怎么样? – 2009-07-08 04:06:09

+0

我已经做了类似的UITextView子类来检测单击和双击 - 它在2.x设备上完美工作,但不在3.0上。 – 2009-07-08 04:21:47

+0

@Reed我希望你的文本视图不会滚动然后。 – 2009-07-08 04:28:55

回答

0

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

UIResponder documentation ...

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

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]; 
}