? ? ? ? 最近寫了一個閱讀器App,產(chǎn)品要求閱讀頁使用仿真翻頁模式栏赴,我使用了蘋果提供的UIPageViewController的仿真翻頁模式,沒有想到系統(tǒng)的也有坑靖秩。遇到的坑都是跟用戶快速翻頁有關(guān)须眷。
坑一:快速翻頁出現(xiàn)閃退
當(dāng)快速翻閱的時候,出現(xiàn)下面的閃退日志
Received CA callback for state, but active state queue is empty'?
(
????0?? CoreFoundation??????????????????????0x000000010780df45 __exceptionPreprocess + 165
????1?? libobjc.A.dylib???????????????????? 0x000000010a81cdeb objc_exception_throw + 48
????2?? CoreFoundation??????????????????????0x000000010780ddaa +[NSException raise:format:arguments:] + 106
????3?? Foundation??????????????????????????0x0000000107f5a5ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
????4?? UIKit?????????????????????????????? 0x000000010911a5c7 -[_UIPageCurl _pageCurlAnimationDidStop:withState:] + 473
????5?? UIKit?????????????????????????????? 0x00000001091146d1 -[_UIPageCurlState animationDidStop:finished:] + 106
????6?? QuartzCore??????????????????????????0x000000010b248fa0 _ZN2CA5Layer23run_animation_callbacksEPv + 308
????7?? libdispatch.dylib?????????????????? 0x000000010b38e49b _dispatch_client_callout + 8
????8?? libdispatch.dylib?????????????????? 0x000000010b3762af _dispatch_main_queue_callback_4CF + 1738
????9?? CoreFoundation??????????????????????0x000000010776e2e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
????10??CoreFoundation??????????????????????0x000000010772f8a9 __CFRunLoopRun + 2073
????11??CoreFoundation??????????????????????0x000000010772ee08 CFRunLoopRunSpecific + 488
????12??GraphicsServices????????????????????0x000000010cb08ad2 GSEventRunModal + 161
????13??UIKit?????????????????????????????? 0x00000001089c230d UIApplicationMain + 171
????14????á????à??????????????????????????? 0x0000000105afd32f main + 111
????15??libdyld.dylib?????????????????????? 0x000000010a17e92d start + 1
????16????????????????????????????????????? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminate_handler unexpectedly threw an exception
當(dāng)修改了取出設(shè)置當(dāng)前閱讀頁的動畫時沟突,在一定程度上緩解了閃退的出現(xiàn)花颗,代碼如下:
[_pageViewController setViewControllers:@[[self readViewWithPage:_currentPage]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
你可能還會遇到其他關(guān)于動畫或者內(nèi)存閃退的日志,都可以這樣處理惠拭。
坑二:快速左右翻頁出現(xiàn)頁面錯亂
? ? ? ? 我的應(yīng)用中有遇到一個這樣的bug,當(dāng)你快速向前翻頁然后又快速向后翻頁扩劝,就會出現(xiàn)向前翻的某一頁一直出現(xiàn)的現(xiàn)象,繼續(xù)操作职辅,會出現(xiàn)你想不到的bug現(xiàn)象棒呛。這是蘋果控件本身緩存的一個bug,研究了很久域携,沒有更好的辦法簇秒,想到的只是讓用戶翻頁速度可控,就是控制用戶的點(diǎn)擊速度涵亏,思路就像大家經(jīng)常會用到的不讓按鈕連續(xù)點(diǎn)擊一樣宰睡,或者是控制在連續(xù)點(diǎn)擊時間間隔為一個固定的數(shù)值蒲凶,比如1秒內(nèi)點(diǎn)擊一次,等等拆内。
我的代碼處理如下:
#pragma mark - UIPageViewController DataSource
- (nullableUIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerBeforeViewController:(UIViewController*)viewController {
? ? WeakSelf
? ? _pageViewController.view.userInteractionEnabled = NO;//限制UIPageViewController上的手勢事件
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? weakSelf.pageViewController.view.userInteractionEnabled = YES;//恢復(fù)UIPageViewController上的手勢事件
? ? });
..........
}
- (nullableUIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerAfterViewController:(UIViewController*)viewController {
? ? WeakSelf
? ? _pageViewController.view.userInteractionEnabled = NO;//限制UIPageViewController上的手勢事件
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? ? weakSelf.pageViewController.view.userInteractionEnabled = YES;//恢復(fù)UIPageViewController上的手勢事件
? ? });
..........
}
上面代碼的處理效果是實(shí)現(xiàn)0.5內(nèi)允許用戶翻頁一次旋圆,這樣就避免了用戶翻頁過快,也就避免了翻頁過快帶來的一系列異常的現(xiàn)象麸恍。
如果有大神有更好的處理方法灵巧,解決這個bug也煩請留言,大家共同探討抹沪,共同學(xué)習(xí)~