我刚刚与iPhone开发陈述,似乎无法找到我要寻找什么,我想做的答案。

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

在c#我将不得不东西看起来像

按钮B =新按钮(); 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];

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

感谢

有帮助吗?

解决方案

虽然不是完整的回答你的问题,我认为下面的解决方案可能是更好然后做“隐形键式解决办法” .. 简单地从子类的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."

}

希望这有助于...

其他提示

我有像“约”来打开约视图一些图标的MAINVIEW。 在相应的控制器(MAINVIEW)予添加的touchesBegan事件处理程序。 此外,我需要检查该观点得到了“感动”,因为有在视图上更多的图标。 我的UIImageView实例是“aboutImg”和用户交互需要被检查。(即UI生成器 - >属性的视图检查员)

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

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

只需使用一个自定义的UIButton与图像作为背景图像。

我还没有找到一种方法来直接与UIImageView做到这一点。但是,如果你继承它,并覆盖nextResponder方法来回报您的控制下的委托,你可以做你以后。暴露委托作为子类的属性,然后可以在用于触摸事件的委托通过,并在任何时候改变它。

我看到其他的答案建议把一个不可见按钮的UIImageView下方,设置图像视图userInteractionEnabled: NO,并让接触穿过到按钮。

您需要什么用的UIImageView做,你可以使用的UIButton与类型的自定义和触摸的内心操作添加到它...设置button.imageview setImage: I-E:

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,然后使用触摸事件。

要onClick事件添加到的UIImageView或一个UIView最简单方法是敲击手势识别添加到它。

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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top