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

私はUITextviewをそのようにサブクラス化しましたが、これはIOS5.0.1でも機能するようです。重要なのは、touchesEndedだけでなく、touchesBeganもオーバーライドすることです(これは私が本当に興味を持っていることです)。

@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 に答える
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 に答える
0

メソッドをオーバーライドすることで切り取り/コピー/貼り付けをオフにできるcanPerformAction:withSender:ため、許可したくないすべてのアクションに対して NO を返すことができます。

UIResponderのドキュメントを参照してください...

うまくいけば、あなたのタッチが食べられるのを防ぐことができます.

于 2009-07-08T12:59:55.980 に答える