代码语言:javascript复制
// 1. 创建一个点击事件,点击时触发labelClick方法
UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
// 2. 将点击事件添加到label上
[label addGestureRecognizer:labelTapGestureRecognizer];
label.userInteractionEnabled = YES; // 可以理解为设置label可被点击
// 3. 在此方法中设置点击label后要触发的操作
- (void)labelClick {
}