将 GestureRecogniser 附加到多个图像视图 [英] Attach GestureRecogniser to multiple imageviews

查看:20
本文介绍了将 GestureRecogniser 附加到多个图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在将相同的手势识别器附加到多个图像视图时遇到了一些奇怪的事情.它只附加到最后一个,换句话说,它只能附加到一个视图!

Something strange I encountered today while attaching same gesture recogniser to multiple image views. It gets attached to only the last one, in other words, it can be attached to only one view!

我不得不创建多个手势识别器来满足我的要求.

I had to create multiple gesture recognisers to meet my requirements.

以下是我所做的.我做得对吗?这是将识别器附加到多个图像视图的唯一方法吗?

Following is what I have done. Am I doing correct? Is that's the only way to attach recognisers to the multiple imageviews?

请注意,我不想使用 UITableView 或 UIVIew 并将所有图像视图放入其中并将手势识别器仅附加到 UITableView 或 UIVIew.我有所有的图像分散,我必须检测哪个图像被拖动.谢谢.

Please note that I don't want to use UITableView or UIVIew and put all imageviews in it and attach gesture recogniser to only UITableView or UIVIew. I have all image scattered and I have to detect which image is being dragged. Thanks.

[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];

推荐答案

是的,每个手势识别器一个视图.所以如果你只想要一个识别器,把它放在超级视图上,例如:

Yes, one view per gesture recognizer. So if you want only one recognizer, put it on the superview, e.g.:

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

然后,在您的处理程序中,您可以:

And then, in your handler, you can:

- (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;
            }
        }
    }
}

顺便说一句,如果您这样做,您也无需担心在图像上启用用户交互.

By the way, if you do that, you don't need to worry about enabling user interaction on the images, either.

最后,您无需担心指定手势识别器的委托,除非您要符合 UIGestureRecognizerDelegate,但事实并非如此.另请注意,我正在为我的识别器使用本地变量,因为没有理由坚持使用它.

Finally, you don't need to worry about specifying your gesture recognizer's delegate unless you're going to conform to UIGestureRecognizerDelegate, which this isn't. Also note that I'm using a local var for my recognizer because there's no reason to hang onto it.

更新:

虽然上面的代码工作正常,但也许更好的是自定义长按手势识别器,如果长按没有发生在图像上,它就会失败(这样它更有可能在你有其他手势识别器发生在您的视图中).所以:

While the above code works fine, perhaps even better would be a custom long press gesture recognizer that would fail if the long press didn't take place over an image (this way it's more likely to play well in case you have other gesture recognizers taking place in your view). So:

#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

然后使用这个新的子类相应地创建您的手势识别器:

then create your gesture recognizer accordingly, using this new subclass:

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

然后,作为这种子类化的一个不错的小好处,您的主要手势识别器得到了简化,即:

and then, as a nice little benefit of this subclassing, your main gesture recognizer is simplified, namely:

- (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];
                         }];
    }
}

这篇关于将 GestureRecogniser 附加到多个图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆