一、前言
1、一個項目中總會有出現(xiàn)界面跳轉(zhuǎn),常見的就是應(yīng)用內(nèi)跳轉(zhuǎn)、Push、Modal、Segue,或者復(fù)雜的嵌套,考慮到方便項目的維護以及功能拓展,我覺得很有必要統(tǒng)一管理,本框架中的Facade類 就是管理所有跳轉(zhuǎn)事件,其中
Facade是繼承自NSObject的單例。2、統(tǒng)一管理一來方便功能拓展;二來整個項目可以保持統(tǒng)一代碼風(fēng)格,相對來說,可維護性更強;而且由于
Facade是繼承自NSObject的單例,因此不依賴于控制器,耦合性更低,可以在任意類中實現(xiàn)跳轉(zhuǎn)。3、本框架著重封裝了應(yīng)用內(nèi)跳轉(zhuǎn)、Push和Modal方式,新增Embed方式,實現(xiàn)控制器嵌套跳轉(zhuǎn)。至于Segue方式考慮到靈活性很差,項目中使用頻率也低,因此不做封裝。
二、應(yīng)用內(nèi)跳轉(zhuǎn)
應(yīng)用類跳轉(zhuǎn)如果細(xì)分的話,可以分為跳轉(zhuǎn)到蘋果商店和其他App
-
1、普通app(App Store以外)跳轉(zhuǎn)
- (void)openAppWithUrlScheme:(NSString *)urlScheme params:(NSDictionary<NSString *, id> *)params complete:(void(^)(BOOL success))complete;(1)跳轉(zhuǎn)前需要配置
URL Schemes,這個就是跳轉(zhuǎn)的url地址了,當(dāng)然iOS 9.0 之后還需要配置白名單,在info.plist中配置LSApplicationQueriesSchemes,在iOS 10.0之后,新出跳轉(zhuǎn)api :- (void)openURL:options:completionHandler:,相比之前的- (BOOL)openURL:,實際上只是多了個options參數(shù),options中的key:
UIApplicationOpenURLOptionUniversalLinksOnly,可以設(shè)置布爾值,如果設(shè)置為YES,則只能打開應(yīng)用里配置好的有效通用鏈接,此時如果沒配置scheme,那么handler中就返回NO,本框架中默認(rèn)使用系統(tǒng)的,相當(dāng)于- (BOOL)openURL:用法。具體區(qū)別請自行查詢,不詳細(xì)分析。(2)值得提一下的是,app跳轉(zhuǎn)一般需要進行參數(shù)傳遞,默認(rèn)只能通過
URL拼接方式或者通過UIPasteboard(不建議),什么情況下使用UIPasteboard呢,一般是用于圖片傳遞的時候,不過其實沒必要,本文的做法是通過將UIImage對象轉(zhuǎn)成NSString,然后進行參數(shù)拼接,其中本框架中還處理了:- 默認(rèn)
urlScheme只需要傳入配置在info.plist中的URL Schemes即可實現(xiàn)跳轉(zhuǎn),參數(shù)可以通過params傳入,框架會自動進行拼接處理。 - 當(dāng)然你也可以在
urlScheme中拼接參數(shù),此時如果params不為空且合法,框架會默認(rèn)在urlScheme中繼續(xù)拼接,并實現(xiàn)跳轉(zhuǎn)。 - 如果此時自行拼接的參數(shù)和傳入的
params重復(fù)key,會以params為準(zhǔn),但跳轉(zhuǎn)后的url不會進行裁剪,可以通過框架的- (NSDictionary *)paramsByOpenAppWithUrl:獲取傳入的參數(shù)。
- 默認(rèn)
(3)關(guān)鍵代碼如下:(邏輯都比較簡單,不詳細(xì)說明)
- (void)openAppWithUrlScheme:(NSString *)urlScheme params:(NSDictionary<NSString *, id> *)params complete:(void(^)(BOOL success))complete {
if (!urlScheme.isNotBlank) return;
NSURL *url = [self urlWithScheme:urlScheme params:params];
if (!url) return;
if ([APPLICATION canOpenURL:url]) {
if ([[[UIDevice currentDevice] systemVersion] compare:@"10.0" options:NSNumericSearch] == NSOrderedAscending) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
BOOL success = [APPLICATION openURL:url];
#pragma clang diagnostic pop
if (complete) {
complete(success);
}
}
else {
[APPLICATION openURL:url options:@{} completionHandler:^(BOOL success) {
if (complete) {
complete(success);
}
}];
}
}
else {
if (complete) {
complete(NO);
}
}
}
-
2、App Store 跳轉(zhuǎn)
- (void)openAppleStoreWithIdentifier:(NSString *)identifier complete:(void(^)(BOOL success))complete;(1)眾所周知,每個app在
App Store中都有一個唯一的id,可以通過iTunes查看,那么此時只需要知道這個identifier即可實現(xiàn)跳轉(zhuǎn)。(2)跳轉(zhuǎn) App Store 其實也有兩種方式,一種通過
URL跳轉(zhuǎn),一種通過StoreKit實現(xiàn),兩者區(qū)別就是,前者直接跳轉(zhuǎn)到App Store,后者則在應(yīng)用內(nèi)打開,筆者覺得后者體驗效果較優(yōu)而且比較穩(wěn)定,因此本框架中使用后者。而且為了優(yōu)化體驗效果,會先跳轉(zhuǎn)過去,然后再加載數(shù)據(jù)。
三、Push

- 1、先上流程圖,也許你會遇到這些需求:VC_A --》VC_B --》VC_C,此時在某種需要場景下,需要 VC_C --》VC_A。
(下面說的界面刷新是指控制器的生命周期方法再走一遍)(1)、界面不需要刷新,可以直接使用
PopToViewController回去。(2)、此時界面需要刷新,需要傳值回去,并且刷新控制器的生命周期方法。
(3)、此時界面不需要刷新,需要傳值回去,不刷新生命周期方法
- 2、針對上面的第一個需求,如果此時不知道 VC_A 在棧中的下標(biāo)(復(fù)雜界面很有可能,當(dāng)然有辦法算出來),那么就很難通過
PopToViewController回到 VC_A;針對第二個需求,傳值刷新問題,由于是多界面通訊,首先肯定想到是使用通知,但通知相對來說就比較離散化了,一多起來就很不方便管理。
3、上面的需求其實很好解決,或許你也知道,就是使用
navigationController的setViewControllers: animated:方法,通過內(nèi)部封裝,對UINavigationController拓展,外界調(diào)用就十分方便,要實現(xiàn)上面的需求,只需要告訴我,是否需要popBack,此時reload重新刷新控制器,必須popBack為YES才有效。當(dāng)然如果nav棧中不存在該控制器(框架中目前默認(rèn)通過類名判斷是否存在,并不是相同控制器),則執(zhí)行系統(tǒng)Push方法。對于第三個需求,其實只需要通過- (__kindof UIViewController *)viewControllerBy:(Class)vcClass方法即可獲取到棧中控制器,然后即可進行參數(shù)傳遞。-
4、關(guān)鍵代碼(具體代碼自行查看)
- (void)popToIndex:(NSInteger)index thenPushViewController:(UIViewController *)viewController needBack:(BOOL)needBack needReload:(BOOL)needReload animated:(BOOL)animated complete:(void(^)())complete { NSArray *sourceViewControllers = self.viewControllers; if (index >= sourceViewControllers.count || viewController == nil || self.topViewController == viewController) { return; } __weak typeof(self) weakSelf = self; [self dispatch_afterViewControllerTransitionComplete:^{ __strong typeof(weakSelf) strongSelf = weakSelf; NSMutableArray<UIViewController *> *arrM = [NSMutableArray arrayWithArray:sourceViewControllers]; [sourceViewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (idx > index) { [arrM removeObject:obj]; } }]; if (needBack) { if (needReload) { [strongSelf setViewControllers:arrM animated:animated]; if ([arrM.lastObject isKindOfClass:[viewController class]]) { [arrM removeLastObject]; } [arrM addObject:viewController]; [strongSelf setViewControllers:arrM animated:NO]; } else { [strongSelf setViewControllers:arrM animated:animated]; } } else { [arrM addObject:viewController]; [strongSelf setViewControllers:arrM animated:animated]; } }]; if (complete) { complete(); } }
三、Modal

拋開需求談功能都是不切實際,如上圖,需求很簡單,就是要 present兩層后,指定dismiss回到首層控制器,那很簡單,dismiss兩次就好了。但這樣的效果會很難受,實際上,我們只需要獲取到指定回到控制器的presentedViewController,然后調(diào)用一下 dismiss 就好,那么如何實現(xiàn)呢?
1、參考系統(tǒng)導(dǎo)航控制器
UINavigationController的做法,通過一個數(shù)組去控制管理,命名為FLPresentStackController,因此對外API基本一致2、用法也和
UINavigationController類似,初始化傳入rootViewController,當(dāng)然,為了適配系統(tǒng)present,框架中做了適應(yīng),當(dāng)不存在FLPresentStackController的時候,就相當(dāng)于系統(tǒng) modal 用法。3、具體實現(xiàn)思路是,在
FLPresentStackController中維護一個數(shù)組棧,當(dāng)調(diào)用presentordismiss的時候,會對這個數(shù)組進行操作,進入實現(xiàn)多層dismiss,跟導(dǎo)航控制器的做法是一樣的。-
4、為了優(yōu)化體驗效果,使用的時候有個注意點,最后present的控制器中的視圖控件,需要添加到
presentContentView中,此時dismiss的時候就不會有視覺差,當(dāng)然,如果你有更優(yōu)的方案,歡迎留言。@property (nonatomic, strong, readonly) UIView *presentContentView; -
5、關(guān)鍵代碼如下:
- (void)dismissToIndex:(NSInteger)index animated: (BOOL)flag completion: (void (^)(void))completion { if (self.statckControllers && self.statckControllers.count && index >= 0 && index < self.statckControllers.count) { NSInteger nextIndex = index + 1; if (nextIndex >= self.statckControllers.count) { return; } UIView *contentView = self.topViewController.presentContentView; UIViewController *currentViewController = self.statckControllers[index]; UIViewController *nextViewController = self.statckControllers[nextIndex]; if (contentView) { [nextViewController.view addSubview:contentView]; [nextViewController.view bringSubviewToFront:contentView]; } [currentViewController dismissViewControllerAnimated:flag completion:^{ [contentView removeFromSuperview]; }]; NSArray<UIViewController *> *tempArr = [NSArray arrayWithArray:self.statckControllers]; [tempArr enumerateObjectsUsingBlock:^(UIViewController * _Nonnull vc, NSUInteger idx, BOOL * _Nonnull stop) { if (idx > index) { self.topViewController.presentStackController = nil; [self.statckControllers removeObject:vc]; } }]; if (completion) { completion(); } } }
四、Embed

為了提高用戶體驗,自定義轉(zhuǎn)場動畫是很常見的手段,這里并不是自定義modal,這個是我自己理解的一種轉(zhuǎn)場方式,其實就是嵌套控制器,并且提供多種轉(zhuǎn)場動畫。實現(xiàn)起來很簡單,代碼也比較簡單,大家自行查看源碼。
- 值得提一下,框架中默認(rèn)不能重復(fù)embed相同的控制器(相同類名),關(guān)鍵代碼如下:
- (void)embedViewController:(UIViewController *)vc inParentViewController:(UIViewController *)parentVC animateType:(FLFacadeAnimateType)animateType duration:(NSTimeInterval)duration completion:(void (^)())completion {
if (vc.parentViewController == parentVC || [self isEmbedViewController:vc isExitAt:parentVC needJudgePrecision:NO]) {
return;
}
[parentVC addChildViewController:vc];
[vc willMoveToParentViewController:parentVC];
[self embedView:vc.view atParentView:parentVC.view animateType:animateType];
if (animateType == FLFacadeAnimateTypeNone) {
[vc didMoveToParentViewController:parentVC];
}
else if([self isFadeAnimate:animateType]) {
[self fadeAnimateWithView:vc.view atParentView:parentVC.view animateType:animateType duration:duration isEmbedAnimated:YES completion:^{
[vc didMoveToParentViewController:parentVC];
}];
}
else {
[self transitionWithView:parentVC.view animateType:animateType duration:duration isEmbedAnimated:YES completion:^{
[vc didMoveToParentViewController:parentVC];
}];
}
if (completion) {
completion();
}
}
五、總結(jié)
1、Facade 類繼承自
NSObject,因此理論上來說可以在任何文件中實現(xiàn)跳轉(zhuǎn),前提是app當(dāng)前有控制器并且已經(jīng)加載完畢(本框架是通過UIApplication分類獲取當(dāng)前控制器去實現(xiàn)的)。2、框架是對系統(tǒng)跳轉(zhuǎn)功能進行拓展并統(tǒng)一管理,因此內(nèi)部兼容系統(tǒng)方法(其實都是系統(tǒng)方法),方便處理常見的跳轉(zhuǎn)方式。
3、框架中代碼量不多,而且邏輯比較簡單,因此沒有做詳細(xì)分析,大家如果有什么不明白或者錯漏的地方可以留言或者簡信我。
4、Facade 地址, 喜歡我的文章可以點個贊,關(guān)注我,會不定時更新文章,謝謝。