2013-08-02 190 views
0

我有一個如何使圖像視圖的行爲與按鈕相同的問題。圖像視圖在iPhone中可按鈕點擊按鈕

Ex。在我的.h文件中

-(IBAction)ClickTheImage:(id)sender; 

然後在.m文件中。

-(void)ClickTheImage:(id)sender 
{ 
//something to do with image click. 
} 

但是當我嘗試到IBAction爲連接上我的廈門國際銀行文件,沒有任何事情發生時,我嘗試連接到我的UIImageView。有關如何做到這一點的任何想法?

+0

爲什麼你想使用imageView它可以使用UIButton來實現。 – iProgrammer

回答

0

您可以通過其他方式創建它。創建一個UIButton並將ImageView定義爲UIButton的背景圖像。然後IBAction和UIButton之間的連接將工作..

[button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

或者你爲什麼要以相反的方式使用它?

2

確保在圖像視圖上將userInteractionEnabled設置爲YES。然後創建並添加一個手勢識別如下:

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickTheImage:)]; 
[self.imageView addGestureRecognizer:tapRecognizer]; 
self.tapRecognizer.delegate = self; 

讓你的類符合UIGestureRecognizerDelegate了。

0

只需使用UITapGestureRecognizer

添加GestureRecognizerDelegate上的.h文件。 假設你的ImageView是* img;

UITapGestureRecognizer *TapRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ImageHasTouched:)]; 
[TapRecognizer setDelegate:self]; 
[img setUserInteractionEnable:YES]; 
[img addGestureRecognizer:TapRecognizer]; 


-(void)ImageHasTouched:(TapGestureRecognizer *)Recognizer 
{ 
    UIImageView *TouchedImage=(UIImageView *)[[Recognizer self] view]; 
    //Now when ever your image will be touched this method will get fired. 
    // do what ever you want 
}