触及UIImageView的事件处理程序

mad*_*ik3 5 iphone touch uiimageview

我刚刚接受了iPhone开发的陈述,似乎无法找到答案,我正在寻找我想做的事情.

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

在c#我会有一些看起来像

按钮b =新按钮(); b.点击+ =我的处理程序代码

现在我有这个

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];
Run Code Online (Sandbox Code Playgroud)

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

谢谢

sam*_*sam 9

虽然它不是你的问题的完整答案,但我认为以下解决方案可能比做"隐形按钮解决方法"更好..简单地从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."

}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...


Mic*_*ann 5

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

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

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