Category 给label加扩展类 添加点击事件
- 作者: 麦克斯甜点屋
- 来源: 51数据库
- 2022-08-17
//.h文件
#import <UIKit/UIKit.h>
@interface UILabel (hanhan)
- (void)dianjishijian:(id)target action:(SEL)action;
@end
//.m文件
#import "UILabel+hanhan.h"
@implementation UILabel (hanhan)
- (void)dianjishijian:(id)target action:(SEL)action{
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
[self addGestureRecognizer:tap];
}
@end
//使用
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 100, 100)];
[label dianjishijian:self action:@selector(btn)];
[self.view addSubview:label];
//别忘了导头文件 #import "UILabel+hanhan.h"
推荐阅读
