場(chǎng)景:
1.父視圖添加了左劃手勢(shì)孽鸡,觸發(fā)返回方法
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(statusBack)];
[self.view addGestureRecognizer:panGesture];
2.子視圖添加了UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor colorWithHexString:@"12aaff"]];
[button setTitle:NSLocalizedString(@"creat_wallet_confirm", nil) forState:UIControlStateNormal];
[button setTitleColor:MOWhiteColor forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:16]];
button.layer.masksToBounds = YES;
button.layer.cornerRadius = 3;
[button addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:button];
出現(xiàn)的結(jié)果bug:
每次點(diǎn)擊確認(rèn)按鈕的時(shí)候蹂午,會(huì)觸發(fā)左劃手勢(shì),導(dǎo)致手勢(shì)和觸發(fā)方法同時(shí)運(yùn)行彬碱,pop出當(dāng)前頁(yè)面豆胸。
解決方法:
當(dāng)前頁(yè)面添加 UIGestureRecognizerDelegate 聲明
panGesture.delegate = self
#pragma mark - UIGestureRecognizer delegate -
/*- 解決左劃手勢(shì)和確認(rèn)按鈕沖突bug -*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// 若為UIButton
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UIButton"]) {
// UIButton 不需要響應(yīng) 父視圖的手勢(shì),保證出發(fā)方法 可以正常
return NO;
}
//默認(rèn)都需要響應(yīng)
return YES;
}