2016-10-27 1 views
0

私はUITableViewとそのセルに1つのUITextFieldを持っています。コンテキストメニューを無効にしなくても、UITextfiledで長押しを管理できますか?

私はUITextFieldで長いジェスチャを追加しましたが、動作しません。テキストフィールドで長いジェスチャをタップすると、常にコンテキストメニュー(選択、コピーカット、過去など)が表示されます。

私の質問は、長いジェスチャーとUITextFiledのコンテキストメニューを管理する方法です。

longGesture = [[UILongPressGestureRecognizer alloc] 
             initWithTarget:self action:@selector(handleLongPress:)]; 
longGesture.minimumPressDuration = 2.0; //seconds 
longGesture.delegate = self; 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint p = [gestureRecognizer locationInView:self.tableView]; 

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p]; 
    if (indexPath == nil) { 
     NSLog(@"long press on table view but not on a row"); 
    } else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     NSLog(@"long press on table view at row %ld", indexPath.row); 
    } else { 
     NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state); 
    } 
} 

テーブルビューのデリゲートメソッド

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    Note *obj = [self.dataArr objectAtIndex:indexPath.row]; 

    TableViewCell *Cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (Cell == nil) { 

     Cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
     Cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    else{ 
     Cell.memoField.text = obj.memoRowText; 
    } 

    Cell.memoField.userInteractionEnabled = YES; 
    [Cell.memoField addGestureRecognizer:longGesture]; 
    Cell.memoField.delegate = self; 
    Cell.memoField.tag = indexPath.row; 

    return Cell; 
} 

答えて

1

あなたはコンテキストを示しジェスチャーの間の障害の要件を設定することをお勧めします:私は、コードの下に試してみた

メニューと長押しのジェスチャー。具体的には、メニューレコグナイザが長押しを失敗するようにしたい場合があります(メニューレコグナイザが長い押しを除外するまで待ちます)。コードでは、これを行う1つの方法は、このデリゲートメソッドを実装することです。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)longPress shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)other { 
    if (other.view /* is a text field in the table view */) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

これらの方法は少し混乱する可能性があります。 -[UIGestureRecognizer requireGestureRecognizerToFail:]には「静的な」障害要件を追加できますが、多くの場合、両方のレコグナイザ(この場合など)への参照を簡単に持つ必要はありません。 多くの場合、これで十分です。 しかし、ジェスチャー認識システムでは、「オンザフライ」で障害要件をインストールする機会も与えられます。もし(firstsecondその方法第1及び第2引数である)[second requireFailureOfGestureRecognizer:first]と呼ばれるかのように-gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:からYESを返す

は同じ効果を有します。

-gestureRecognizer:shouldRequireFailureOfGestureRecognizer:からYESを返すOTOHは、[first requireFailureOfGestureRecognizer:second]と同じ効果があります。

関連する問題