美文网首页
iOS-Lable实现长按后复制

iOS-Lable实现长按后复制

作者: Mn_Su | 来源:发表于2016-09-23 13:16 被阅读0次
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 &lt; 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实现长按后复制

      本文链接:https://www.haomeiwen.com/subject/qsutyttx.html