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