Uiimageview response click event in IOS

Time:2021-10-24

Uiimageview response click event in IOS

Sometimes I will click on an image and then let it trigger an event or jump to the view. The first method I think of is to use uibutton and set the background image attribute of the button to the image. The effect is achieved, but it is not the best method. Directly touch the method

Object defining image

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

Response method

-(void)onClickImage{ 
   
  Nslog (@ "picture clicked!"); 
}

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