2011-03-26 29 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:...];