2

同じジェスチャ認識機能を複数の画像ビューにアタッチしているときに、今日遭遇した奇妙なことがあります。最後の1つだけにアタッチされます。つまり、1つのビューにのみアタッチできます。

要件を満たすために、複数のジェスチャ認識機能を作成する必要がありました。

以下は私がしたことです。私は正しいことをしていますか?これが、複数のイメージビューに認識機能をアタッチする唯一の方法ですか?

UITableViewまたはUIVIewを使用せず、すべてのimageviewをその中に配置し、ジェスチャーレコグナイザーをUITableViewまたはUIVIewのみにアタッチすることに注意してください。すべての画像が散らばっていて、どの画像がドラッグされているかを検出する必要があります。ありがとう。

[imgView1 setUserInteractionEnabled:YES];
[imgView1 setMultipleTouchEnabled:YES];

[imgView2 setUserInteractionEnabled:YES];
[imgView2 setMultipleTouchEnabled:YES];

[imgView3 setUserInteractionEnabled:YES];
[imgView3 setMultipleTouchEnabled:YES];

[imgView4 setUserInteractionEnabled:YES];
[imgView4 setMultipleTouchEnabled:YES];

[imgView5 setUserInteractionEnabled:YES];
[imgView5 setMultipleTouchEnabled:YES];

[imgView6 setUserInteractionEnabled:YES];
[imgView6 setMultipleTouchEnabled:YES];


//Attach gesture recognizer to each imagviews
gestureRecognizer1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer1.delegate = self;

gestureRecognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer2.delegate = self;

gestureRecognizer3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer3.delegate = self;

gestureRecognizer4 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer4.delegate = self;

gestureRecognizer5 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer5.delegate = self;

gestureRecognizer6 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer6.delegate = self;

[imgView1 addGestureRecognizer:gestureRecognizer1];
[imgView2 addGestureRecognizer:gestureRecognizer2];
[imgView3 addGestureRecognizer:gestureRecognizer3];
[imgView4 addGestureRecognizer:gestureRecognizer4];
[imgView5 addGestureRecognizer:gestureRecognizer5];
[imgView6 addGestureRecognizer:gestureRecognizer6];
4

3 に答える 3

2

(発見したように)ジェスチャレコグナイザーを複数のオブジェクトにアタッチすることはできません。実行していることの1つの解決策は、UIImageViewをサブクラス化し、そのクラスにセットアップコードを設定して、各ビューが認識機能を作成するようにすることです。

于 2012-07-04T14:22:31.263 に答える
2

はい、ジェスチャレコグナイザーごとに1つのビュー。したがって、レコグナイザーが1つだけ必要な場合は、それをスーパービューに配置します。例:

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
[self.view addGestureRecognizer:gestureRecognizer];

次に、ハンドラーで次のことができます。

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:self.view];

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        for (UIView *view in self.view.subviews)
        {
            if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location))
            {
                UIImageView *image = (UIImageView *) view;

                // ok, now you know which image you received your long press for
                // do whatever you wanted on it at this point

                return;
            }
        }
    }
}

ちなみに、そうすれば、画像でのユーザーインタラクションを有効にすることについても心配する必要はありません。

最後に、に準拠する場合を除いて、ジェスチャレコグナイザーのデリゲートを指定することを心配する必要UIGestureRecognizerDelegateはありません。これはそうではありません。また、認識機能にローカル変数を使用していることにも注意してください。これは、ローカル変数に固執する理由がないためです。

アップデート:

上記のコードは正常に機能しますが、画像上で長押しが行われなかった場合に失敗するカスタムの長押しジェスチャレコグナイザーの方がおそらくさらに良いでしょう(このようにして、他のジェスチャ認識機能を使用している場合にうまく再生される可能性が高くなりますビューに配置します)。それで:

#import <UIKit/UIGestureRecognizerSubclass.h>

@interface ImageLongPressGestureRecognizer : UILongPressGestureRecognizer
@property (nonatomic, weak) UIImageView *imageview;
@end

@implementation ImageLongPressGestureRecognizer

@synthesize imageview = _imageview;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.imageview = nil;

    [super touchesBegan:touches withEvent:event];

    CGPoint location = [self locationInView:self.view];

    for (UIView *view in self.view.subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location))
        {
            self.imageview = (UIImageView *)view;
            return;
        }
    }

    self.state = UIGestureRecognizerStateFailed;
}

@end

次に、この新しいサブクラスを使用して、それに応じてジェスチャレコグナイザーを作成します。

ImageLongPressGestureRecognizer *gestureRecognizer = [[ImageLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:gestureRecognizer];

次に、このサブクラス化のちょっとした利点として、メインのジェスチャレコグナイザーが簡略化されます。

- (void)handleLongPress:(ImageLongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        // you can now do whatever you want with sender.imageview, e.g. this makes it blink for you:

        [UIView animateWithDuration:0.5 
                         animations:^{
                             sender.imageview.alpha = 0.0;
                         } completion:^(BOOL finished){
                             [UIView animateWithDuration:0.5 
                                              animations:^{
                                                  sender.imageview.alpha = 1.0;
                                              } 
                                              completion:nil];
                         }];
    }
}
于 2012-07-04T15:13:09.023 に答える
0

まず、ビューの配列とレコグナイザーの配列(必要に応じて可変配列)を作成してから、データを入力する必要があると思います。サイクルを使用してコードの重複を回避するのに役立ちます。

1つのレコグナイザーを使用した複数のビューについては、いいえ、できません。ここで回答します

于 2012-07-04T14:22:52.280 に答える