SoFunction
Updated on 2025-04-12

UIImageView in IOS responds to click event

UIImageView in IOS responds to click event

Sometimes you will encounter clicking on an image and then letting the image trigger an event, or jumping to the view. The first method that comes to mind is to use UIButton to set the Button's background image attribute to the image. The effect is achieved, but it is not the best method. Direct contact and sending method

Define the object of Image

UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320,100)]; 
   = [UIColor redColor];//Because the image attribute is not set, in order to display the image coverage area  =YES; 
  UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)]; 
  [imgView addGestureRecognizer:singleTap]; 
  [singleTap release]; 
   

Response method

-(void)onClickImage{ 
   
  NSLog(@"The picture was clicked!"); 
} 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!