• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 将 GestureRecogniser 附加到多个 ImageView

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:50:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我必须创建多个手势识别器来满足我的要求。

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

请注意,我不想使用 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 actionselector(gestureHandler];
gestureRecognizer1.delegate = self;

gestureRecognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
gestureRecognizer2.delegate = self;

gestureRecognizer3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
gestureRecognizer3.delegate = self;

gestureRecognizer4 = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
gestureRecognizer4.delegate = self;

gestureRecognizer5 = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
gestureRecognizer5.delegate = self;

gestureRecognizer6 = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
gestureRecognizer6.delegate = self;

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



Best Answer-推荐答案


是的,每个手势识别器一个 View 。因此,如果您只想要一个识别器,请将其放在 superview 上,例如:

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self actionselector(gestureHandler];
[self.view addGestureRecognizer:gestureRecognizer];

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

- (void)handleLongPressUILongPressGestureRecognizer *)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;
            }
        }
    }
}

顺便说一句,如果你这样做了,你也不必担心在图像上启用用户交互。

最后,您无需担心指定手势识别器的委托(delegate),除非您要遵守 UIGestureRecognizerDelegate,但事实并非如此。另请注意,我使用本地 var 作为识别器,因为没有理由坚持使用它。

更新:

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

#import <UIKit/UIGestureRecognizerSubclass.h>

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

@implementation ImageLongPressGestureRecognizer

@synthesize imageview = _imageview;

- (void)touchesBeganNSSet *)touches withEventUIEvent *)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 actionselector(handleLongPress];
[self.view addGestureRecognizer:gestureRecognizer];

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

- (void)handleLongPressImageLongPressGestureRecognizer *)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];
                         }];
    }
}

关于ios - 将 GestureRecogniser 附加到多个 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11330738/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap