2013-04-17 135 views
0

我的目標CI是新的有一個形象,我想展示一些提示信息時,我點擊它像這個 -添加點擊事件圖像

AHolder = [[UIImageView alloc] initWithFrame:CGRectMake(5, 80, 40, 40)]; 
    UIImage *imageA = [UIImage imageNamed:@"A.png"]; 
    AHolder.image = imageA; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aPressed:) name:@"aPressed" object:nil]; 
    [view addSubview:AHolder] 

而且它的事件像這個 -

添加的圖像
-(IBAction)aPressed:(id)sender 
{ 
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil]; 
    [alert show]; 
    [alert release]; 
} 

它不給我任何錯誤,但也當我點擊圖像時什麼也沒有發生。 請給我建議任何解決方案。

回答

2

向圖像視圖添加輕敲手勢。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)]; 
tapGesture.numberOfTapsRequired=1; 
[AHolder setUserInteractionEnabled:YES]; 
[AHolder addGestureRecognizer:tapGesture]; 
[tapGesture release]; 

-(void)handleTapGesture{ 
//do what ever you want here 
} 

否則,只需使用帶背景圖像的按鈕。

0

使用tapGestureRecognizer。所以每當用戶點擊iamgeview,你可以做你想做什麼都

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aPressed)]; 
tapGesture.numberOfTapsRequired=1; 
[aHolder setUserInteractionEnabled:YES]; 
[aHolder addGestureRecognizer:tapGesture]; 

-(void)aPressed{ 
//do what ever you want here 
} 
0

很簡單和最流行的解決方案,您的問題將是使用自定義的UIButton了你的形象。 然後,只需將選擇器設置爲要調用的方法,然後單擊按鈕即可。確保UIButton & UIImage的框架是相同的。

0

在xib文件中添加「圓形按鈕」對象。將'Type'屬性設置爲'custom',將'Background'屬性設置爲'nameofyourimage.extension'。

將aPressed:方法連接到round rect按鈕的事件內部。

0

無需在您的圖片上放置UIButton。只需使用UIButton而不是UIImageView。您可以將UIButtonbackgroundimageimage屬性設置爲所需的圖像。

除此之外,你可以使用UITapGestureRecognizer這樣的:

把這個代碼viewDidLoad

UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)]; 
[yourImage addGestureRecognizer:rec]; 
[yourImage setUserInteractionEnabled:YES]; //!! this is important 

和實現這個委託方法

#define unused_related_to_gesture 
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer 
{ 
    if(recognizer.state == UIGestureRecognizerStateBegan) 
    { 
     NSLog(@"\nUIGestureRecognizerStateBegan\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStateCancelled) 
    { 
     NSLog(@"\nUIGestureRecognizerStateCancelled\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStateChanged) 
    { 
     NSLog(@"\nUIGestureRecognizerStateChanged\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     NSLog(@"\nUIGestureRecognizerStateEnded\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStateFailed) 
    { 
     NSLog(@"\nUIGestureRecognizerStateFailed\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStatePossible) 
    { 
     NSLog(@"\nUIGestureRecognizerStatePossible\n"); 
    } 
    if(recognizer.state == UIGestureRecognizerStateRecognized) 
    { 
     NSLog(@"\nUIGestureRecognizerStateRecognized\n"); 
     // this section will contain your code of clicking the image. 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil]; 
     [alert show]; 
     [alert release]; 
    } 
}