关于ios中UILabel上添加事件

 项目中把imageView替换成UILabel,然后发现label没有点击事件,但是可以加手势,代码如下:

CGRect titleFrame = CGRectMake(0, 55, 300, 15);  

UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];  

titleLabel.text =@“标题”;

  //创建手势实例,并连接方法UITapGestureRecognizer,点击手势  

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickUILable:)];

titleLabel.userInteractionEnabled=YES;

//设置手势点击数,双击:默认不设置 就是1 为单击事件  

tapGesture.numberOfTapsRequired=2;  

//设置手指数 单指  

tapGesture.numberOfTouchesRequired=1; 

// titleLabel添加手势识别  

[titleLabel addGestureRecognizer:tapGesture];


//定义响应事件

-(void)onClickUILable:(UITapGestureRecognizer *)sender{

        UITapGestureRecognizer *tap = (UITapGestureRecognizer*)sender;  

        UILabel *la=(UILabel*)tap.view;  

        //相应代码 

}

你可能感兴趣的:(ios)