4

私の iPad アプリケーションでは、画面上に複数のビューがあります。

私がやりたいことは、ナビゲーション バーにダブル タップ ジェスチャ レコグナイザーを適用することです。しかし、私は成功しませんでしたが、そのビューに同じジェスチャ認識エンジンを適用すると機能します。

私が使用しているコードは次のとおりです。

// Create gesture recognizer, notice the selector method
UITapGestureRecognizer *oneFingerTwoTaps = 
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];

// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];

[self.view addGestureRecognizer:oneFingerTwoTaps];

これはビューで機能しますが、これが完了すると:

[self.navigationController.navigationBar addGestureRecognizer:oneFingerTwoTaps]

動作しません。

4

2 に答える 2

4

このためには、UINavigationBarをサブクラス化し、その中のinitボタンをオーバーライドして、そこにジェスチャレコグナイザーを追加する必要があります。

したがって、「CustomNavigationBar」というサブクラスを作成するとします。mファイルには、次のようなinitメソッドがあります。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder])) 
    {
        UISwipeGestureRecognizer *swipeRight;
        swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
        [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
        [swipeRight setNumberOfTouchesRequired:1];
        [swipeRight setEnabled:YES];
        [self addGestureRecognizer:swipeRight];
    }
    return self;
}

次に、InterfaceBuilderのナビゲーションバーのクラス名をサブクラスの名前に設定する必要があります。

ここに画像の説明を入力してください

また、ジェスチャの最後に送信されたメソッドをリッスンするために、ナビゲーションバーにデリゲートプロトコルを追加すると便利です。たとえば、上記の場合、右にスワイプします。

@protocol CustomNavigationbarDelegate <NSObject>
    - (void)customNavBarDidFinishSwipeRight;
@end

次に、mファイルで-ジェスチャ認識メソッド(作成したものは何でも)で、このデリゲートメソッドをトリガーできます。

お役に立てれば

于 2012-06-23T05:46:39.013 に答える