iOS开发小技巧

使用YYLabel时遇到点击事件被延迟

2018-11-02  本文已影响1人  slowdony

在使用YYLabel时

- (YYLabel *)repeatLab{
    if(!_repeatLab){
        _repeatLab = [[YYLabel alloc] initWithFrame:CGRectMake(0 , 100 , ScreenWidth-20, 20)];
        _repeatLab.textColor = TITLE_COLOR;
        _repeatLab.userInteractionEnabled = YES;
        NSString *str = @"重新发送这个按钮哦";
        NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:str];
        attrStr.yy_font = SDFONT;
        attrStr.yy_color =  [UIColor colorWithHexString:@"808080" alpha:1];
        attrStr.yy_alignment = NSTextAlignmentRight;
        
        [attrStr yy_setTextHighlightRange:NSMakeRange(0, 4) color:[UIColor blueColor] backgroundColor:[UIColor redColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            DLog(@"点击重新发送");
            [MessageView showMessage:@"点击重新发送"];
        }];
        self.repeatLab.attributedText =attrStr;
    }
    return _repeatLab;
}

然后在viewDidLoad里 [self.view addSubview:self.repeatLab];
是不是感觉很完美,但是当你开心的点击重新发送时,你发现点击事件不响应,然后你以为你哪错了,然后你查看源代码后发现没有错啊,于是你有重复的点了几次,最后发现点击事件延迟了,按住不放停几秒后才会响应点击事件
然后你一点一点查看代码
最后发现[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)]];
然后注释掉发现延迟取消了

上一篇下一篇

猜你喜欢

热点阅读