【问题标题】:touches event handler for UIImageView触摸 UIImageView 的事件处理程序
【发布时间】:2010-03-11 17:58:42
【问题描述】:

我刚刚了解 iPhone 开发,但似乎无法找到我想要做的事情的答案。

看来我应该能够以编程方式创建一个 UIImageView,然后为它的触摸功能设置一个事件处理程序。

在 c# 中我会有一些看起来像的东西

按钮 b = new Button(); b.Click+= 我的处理程序代码

我现在有这个

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 141.0f, 151.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

myImage.userInteractionEnabled = YES;
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];

我需要做什么来覆盖触摸事件?

谢谢

【问题讨论】:

    标签: iphone uiimageview touch


    【解决方案1】:

    虽然这不是您问题的完整答案,但我认为以下解决方案可能比执行“隐形按钮解决方法”更好。 只需从 UIImageView 子类化并覆盖以下方法:

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
     //your UIImageView has been touched :)
    
     //event -> "A UIEvent object representing the event to which the touches belong."
    
     //touches -> "A set of UITouch instances in the event represented by event that    represent the touches in the UITouchPhaseEnded phase."
    
    }
    

    希望这会有所帮助...

    【讨论】:

    • 真的这不是我要找的答案。但似乎我能找到的唯一方法是使用继承。我想 c# 已经把我宠坏了,以至于我认为这样的方法应该可以在没有那么多工作的情况下使用。不过,谢谢。
    【解决方案2】:

    我有一个带有一些图标的主视图,例如“关于”来打开一个关于视图。 在相应的控制器(主视图)中,我为 touchesBegan 添加了一个事件处理程序。 此外,我需要检查哪个视图已被“触摸”,因为视图上有更多图标。 我的 UIImageView 实例是“aboutImg”,需要检查用户交互(即 UI 构建器 -> 视图属性检查器)。

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        if ([touch view] == aboutImg){
            [self ShowAbout];
        }
    }
    

    确保选中“已启用用户交互”。这是最简单的解决方案

    【讨论】:

    • 这是最简单的解决方案。我检查了用户交互复选框并将其放入子类 UITableViewCell 以获取单元格中的多个触摸。工作完美而轻松。需要继承 UIImageView
    【解决方案3】:

    只需使用带有图像的自定义 UIButton 作为背景图像。

    【讨论】:

      【解决方案4】:

      我还没有找到直接使用UIImageView 的方法。但是,如果您将其子类化并覆盖 nextResponder 方法以在您的控制下返回一个委托,您就可以做您想做的事情。将委托作为子类的属性公开,然后您可以为触摸事件传入委托并随时更改它。

      【讨论】:

        【解决方案5】:

        我看到其他答案建议在 UIImageView 下方放置一个不可见按钮,将图像视图设置为 userInteractionEnabled: NO,并让触摸传递到按钮。

        【讨论】:

          【解决方案6】:

          你不需要与 UIImageView 有任何关系,你可以使用自定义类型的 UIButton 并添加内部动作的修饰...set button.imageview setImage: 即:

          UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
          btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
          UIImage *img = [UIImage imageNamed:@"myimage.png"];
          [button setImage:img forState:UIControlStateNormal];
          

          如果你想使用 UIImageView,那么你需要在你的 imageview 中添加手势。否则,您可以将 UIImageView 子类化,然后使用触摸事件。

          【讨论】:

            【解决方案7】:

            向 UIImageView 或 UIView 添加 onClick 事件的最简单方法是向其添加轻击手势识别器。

            UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
            UIImageView *myImageView = [[UIImageView alloc] initWithImage: myImage];
            
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action: @selector(onImageViewClicked)];
            singleTap.numberOfTapsRequired = 1;
            singleTap.numberOfTouchesRequired = 1;
            [self.myImageView addGestureRecognizer:singleTap];
            [self.myImageView setUserInteractionEnabled:YES];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-28
              • 2011-10-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-05-09
              相关资源
              最近更新 更多