1.声明相关属性,和代理
<UIGestureRecognizerDelegate>
@property(nonatomic,strong) UILabel *moonLab;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIView *PopView;
2.创建一个lable
self.moonLab = [[UILabel alloc]init];
self.moonLab.font = [UIFont systemFontOfSize:14];
self.moonLab.backgroundColor = [UIColor yellowColor];
self.moonLab.text = @"昨晚睡得很好,请专家团给点建议!--给你妹的建议,滚去睡觉!昨晚睡得很好,请专家团给点建议!--给你妹的建议,滚去睡觉!";
self.moonLab.textColor = TEXTCOLOR;
self.moonLab.lineBreakMode = NSLineBreakByWordWrapping;
self.moonLab.numberOfLines = 0;
CGSize textSize = [self sizeWithText:self.moonLab.text font:self.moonLab.font maxSize:CGSizeMake(200, MAXFLOAT)];
self.moonLab.frame = CGRectMake(15+30+20,34, textSize.width+45, textSize.height);
[tempView addSubview:self.moonLab];
UILongPressGestureRecognizer *toucP = [[UILongPressGestureRecognizer alloc]init];
[toucP addTarget:self action:@selector(longPcLICK:)];
toucP.minimumPressDuration = 1;
toucP.delegate = self;
[self.moonLab addGestureRecognizer:toucP];
self.moonLab.userInteractionEnabled = YES;
3.实现手势目标事件
- (void)longPcLICK:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan){
self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
[self.view addSubview:self.bgView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
tap.delegate = self;
[self.bgView addGestureRecognizer:tap];
self.PopView = [[UIView alloc]initWithFrame:CGRectMake(50, HEIGHT/2+20,WIDTH-100, HEIGHT/13*2)];
self.PopView.backgroundColor = WHITECOLOR;
self.PopView.layer.cornerRadius = 3;
[self.bgView addSubview:self.PopView];
NSArray *arr = @[@"复制",@"删除"];
for (int i = 0; i < arr.count; i++) {
UIButton *warnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
warnBtn.frame = CGRectMake(0, i*(HEIGHT/13), WIDTH-100, HEIGHT/13);
warnBtn.layer.cornerRadius = 1;
warnBtn.clipsToBounds = YES;
warnBtn.tag = i+1992;
warnBtn.adjustsImageWhenHighlighted = NO;
warnBtn.layer.borderColor = TEXTTINTCOLOR.CGColor;
warnBtn.layer.borderWidth = 1.2;
warnBtn.layer.masksToBounds = YES;
[warnBtn setTitle:arr[i] forState:UIControlStateNormal];
[warnBtn setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
warnBtn.titleLabel.font = [UIFont systemFontOfSize:16];
[warnBtn addTarget:self action:@selector(warnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.PopView addSubview:warnBtn];
}
}
}
4.实现轻点屏幕,弹出视图消失方法
- (void)tapClick:(UITapGestureRecognizer *)sender{
if (sender.numberOfTapsRequired== 1) {
[_bgView removeFromSuperview];
}
}
iOS-Lable实现长按后复制
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 先上图,这是最终功能实现效果图 其实很简单,首先创建一个继承了UILabel的MLLongPressLabel,然...