UILable添加事件

原文:http://blog.sina.com.cn/s/blog_9e8867eb0101dk6t.html

先需要声明的是:UILabel或UIImageView的 userInteractionEnabled属性默认为no,也就是说默认不接受事件。

 
所以方法一:
label.userInteractionEnabled = YES;//设置userInteractionEnabled属性为yes。

UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];//初始化一个单击手势

[label addGestureRecognizer:labelTap];//给label添加单击手势
- (void)click:(UITapGestureRecognizer *)gesture{
    
} 

方法二:添加一个他们的子类,重写view的touch方法

#import


@interface untitled : UIImageView {

}

@end

#import "untitled.h"


@implementation untitled


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.userInteractionEnabled=YES;
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)dealloc {
    [super dealloc];
}


@end


在touch事件种添加你自己想要的操作就可以了
然后定义自己的image就用untitled就可以了 

原文地址:https://www.cnblogs.com/SimonGao/p/4552604.html