最近連著兩個(gè)版本被interactivePopGestureRecognizer坑,真的是被坑死了。
界面卡死的問(wèn)題
這個(gè)問(wèn)題查好了好幾個(gè)晚上。表現(xiàn)是在root view controller亂滑時(shí),容易卡死,進(jìn)入后臺(tái)一下再回來(lái)又會(huì)正常。
做了各種嘗試,排查了動(dòng)畫(huà)、內(nèi)存和基礎(chǔ)組件等可能的原因,無(wú)果。在近乎絕望的情況下,突然靈光一現(xiàn),想起自己重構(gòu)代碼時(shí),刪除了幾行關(guān)于導(dǎo)航代理的代碼。
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if (viewController == navigationController.viewControllers[0])
{
navigationController.interactivePopGestureRecognizer.enabled = NO;
}else {
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
顯示root view controller時(shí),關(guān)閉掉interactivePopGestureRecognizer這個(gè)手勢(shì)。
按鈕點(diǎn)擊無(wú)反應(yīng)問(wèn)題
為了提高靈活性,來(lái)往支持view controller關(guān)閉右滑手勢(shì)。為了提高性能,我用gestureRecognizerShouldBegin替換了shouldReceiveTouch。結(jié)果引入了一個(gè)我意想不到的bug。
點(diǎn)擊發(fā)語(yǔ)音消息的按鈕,反映遲鈍。經(jīng)過(guò)無(wú)數(shù)種嘗試,我發(fā)現(xiàn)了一些規(guī)律,按鈕如果在底下的時(shí)候,左半邊點(diǎn)擊響應(yīng)遲鈍,右半邊卻很靈敏。但是在別的地方一直都會(huì)很靈敏。我重點(diǎn)排查了view controller和輸入框上別的手勢(shì),清空所有的手勢(shì)依舊如此。
很難想象這是導(dǎo)航欄的手勢(shì)導(dǎo)致的。觸點(diǎn)在輸入框不靈敏的區(qū)域時(shí),如果能在shouldReceiveTouch快速返回NO的話,點(diǎn)擊就非常靈敏了。
@interface NavigationController : UINavigationController <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@end
@implementation NavigationController
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
{
//如果在紅色方框內(nèi)有長(zhǎng)按手勢(shì),這里需要快速返回NO,要不然反映會(huì)很遲鈍。
return YES;
}
@end
這種情況下點(diǎn)擊按鈕毫無(wú)壓力,非常靈敏。
這種情況下,按住紅色方框區(qū)域,按鈕會(huì)遲遲收不到touchesBegan,按鈕不會(huì)進(jìn)入highlighted狀態(tài)。