2009-07-08 213 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]; 
}