UIView添加点击事件

作者: iOS 发布时间: 2021-03-30 浏览: 2207 次 编辑

使用 UITapGestureRecognizer ,创建一个 UITapGestureRecognizer 将它添加到view上即可

事例代码

   UIView *tapView=[[UIView alloc]initWithFrame:SCREEN_FRAME];

    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    [tapView addGestureRecognizer:tapGesturRecognizer];


   -(void)tapAction:(id)tap
  {
           NSLog(@"点击了tapView");
  }