这几天遇到一个很有意思也很脑门的事情,- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event,该方法点击一次却连调两次,甚是郁闷至极。
场景1:
KLLabel : UILabel
UILabel (YBAttributeTextTapAction)
在KLLabel的实例调用其分类的方法
//评论内容
KLLabel* commentLab1 = [[KLLabel alloc]init];
commentLab1.hashtagLinkTapHandler = ^(KLLabel *label, NSString *string, NSRange range){
if (_topicNameBlock) {
_topicNameBlock(string);
}
NSLog(@"hashtagLinkTapHandler");
};
commentLab1.userHandleLinkTapHandler = ^(KLLabel *label, NSString *string, NSRange range) {
if (_aterNicknameBlock) {
_aterNicknameBlock([string stringByReplacingOccurrencesOfString:@"@" withString:@""]);
}
};
commentLab1.urlLinkTapHandler = ^(KLLabel *label, NSString *string, NSRange range) {
//shareType不为空即可分享
NSString* urlStr = nil;
if ([string hasPrefix:@"www"]) {
urlStr = [NSString stringWithFormat:@"https://%@",string];
}else{
urlStr = string;
}
JJNavigationController * nav = [ProjectManager sharedInstance].getTabSelectedNav;
WebViewController * webVC = [[WebViewController alloc]init];
webVC = [webHelper getWebVC:urlStr];
[nav pushViewController:webVC animated:YES];
};
这个是回调KLLabel没有被选中的区域
_commentLab1.jumpVcBlock = ^(){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //dispatch_get_main_queue()
if (!_commentFlag) {
dispatch_async(dispatch_get_main_queue(), ^{
_tapUpview();//点击正文回调
_commentFlag = NO;
});
}else{
_commentFlag = NO;
}
});
};
这个是YBAttributeTextTapAction分类回调昵称的区域
if ([replyModel1.to_user_id isEqualToString:@"0"]) {
[_commentLab1 yb_addAttributeTapActionWithStrings:@[name] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
_commentFlag = YES;
_replyTitleView(replyModel1.user_id,name);
}];
}else{
[_commentLab1 yb_addAttributeTapActionWithStrings:@[replyModel1.user_name,replyModel1.to_user_name] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
if (index == 0) {
_commentFlag = YES;
_replyTitleView(replyModel1.user_id,name);
}else{
_commentFlag = YES;
_replyTitleView(replyModel1.to_user_id,toname);
}
}];
}
//KLLabel
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_isTouchMoved = NO;
// Get the info for the touched link if there is one
NSDictionary *touchedLink;
CGPoint touchLocation = [[touches anyObject] locationInView:self];
touchedLink = [self linkAtPoint:touchLocation];
if (touchedLink)
{
self.selectedRange = [[touchedLink objectForKey:KILabelRangeKey] rangeValue];
if (_urlLinkTapHandler) {//life 他自己不调,商会聊天页面,所以判断是url后主动调用 //此问题已解决,暂留life
// [self touchesEnded:touches withEvent:event];
}
}
else
{
// [super touchesBegan:touches withEvent:event];
}
}
//UILabel (YBAttributeTextTapAction)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (!self.isTapAction) {
return;
}
if (objc_getAssociatedObject(self, @selector(enabledTapEffect))) {
self.isTapEffect = self.enabledTapEffect;
}
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
__weak typeof(self) weakSelf = self;
[self yb_getTapFrameWithTouchPoint:point result:^(NSString *string, NSRange range, NSInteger index) {
if (weakSelf.tapBlock) {
weakSelf.tapBlock (string , range , index);
}
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(yb_attributeTapReturnString:range:index:)]) {
[weakSelf.delegate yb_attributeTapReturnString:string range:range index:index];
}
if (self.isTapEffect) {
[self yb_saveEffectDicWithRange:range];
[self yb_tapEffectWithStatus:YES];
}
}];
}
问题来了:
- 长按事件代码断点路径
kllabel
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
-》分两种情况
@1 :调了 [super touchesBegan:touches withEvent:event]; 就会继续走到UILabel (YBAttributeTextTapAction)的- (void)touchesBegan:
@2:没调的话则不会走到
2.单击事件代码断点路径
kllabel
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
-》分两种情况
@1 :调了 [super touchesBegan:touches withEvent:event]; 就会继续走到UILabel (YBAttributeTextTapAction)的- (void)touchesBegan:,而且连续调两次
@2:没调的话则只调一次后面的
3.单击且不调 [super touchesBegan:touches withEvent:event],那么UILabel (YBAttributeTextTapAction)的- (void)touchesBegan:就只调用一次,但是会先回调KLLabel的没有事件区域jumpVcBlock回调,然后才回到YBAttributeTextTapAction的yb_addAttributeTapActionWithStrings回调,这么一来就会先去详情才去profile,所以还是得判断,而且判断逻辑就如上述,延迟0.15S的等待时间,其实这样很不好,最好的方式是在多线程处理,应该是线程组,等到这一组线程都完成了,才开始下一个动作,待续

网友评论