有时候会遇到点击一张图片,然后让这张图片触发一个事件,或者是跳转视图,想到的第一个方法就是用UIButton,将Button的背景图片属性设置为该图片,效果达到了,但不是最好的方法,直接触发方法


定义Image的对象

UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320,100)];  
imgView.backgroundColor = [UIColor redColor];//因为没有设置image属性,为了显示出图片覆盖区域  
imgView.userInteractionEnabled=YES;  
UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickImage)];  
[imgView addGestureRecognizer:singleTap];       


响应方法

-(void)onClickImage{  
      
    NSLog(@"图片被点击!");  
}  




相关文章:

  • 2022-12-23
  • 2021-09-13
  • 2021-03-31
  • 2022-02-04
  • 2021-11-05
  • 2021-05-08
  • 2022-12-23
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案