2011-03-26 40 views
0

我想将事件添加到图像。但如果我添加这意味着它会给出警告:UIImage可能不会响应控件事件的addtarget操作。我如何纠正这段代码。我的代码 UIImageView * image; UIImage * image1 = [UIImage imageNamed:@“left_arrow.png”]; image = [[UIImageView alloc] initWithImage:image1]; image.frame = CGRectMake(100,410,30,30);向xcode中的图像添加事件时出错

[image1 addTarget:self action:@selector(buttonPressed2:)forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:image]; 

回答

2

如果你不想使用额外的按钮,你可以一个UITapGestureRecognizer添加到ImageView的。

像这样:

UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed2:)] autorelease]; 
[imageView addGestureRecognizer:tapGesture]; 
[imageView setUserInteractionEnabled:YES]; 

但是这更像是一个UIControlEventTouchDown事件

+0

fluchtpunkt:感谢它的工作 – Aravindhan 2011-03-26 13:48:58

0

您需要在UIImageView上创建透明的自定义UIButton。该按钮将处理事件。 UIImageView可以做到这一点。

UIButton *btn = [[[UIButton alloc] initWithFrame:imageView.frame] autorelease]; 
btn.buttonType=UIButtonTypeCustom; 
[btn addTarget:...];