說明:
項(xiàng)目中我們可能會(huì)針對全局的 push桦卒,pop邑跪,present播瞳,dismiss 等視圖轉(zhuǎn)場進(jìn)行操作比驻;
例如:
* 給 push/present 的下一級視圖一個(gè)返回按鈕。
* 給 push/present 的過程中隱藏所有的 tabbar敷硅。
* 給 push/present 的時(shí)候自定義一個(gè)轉(zhuǎn)場動(dòng)畫怎棱。
。拥诡。触趴。。渴肉。針對視圖轉(zhuǎn)換之間進(jìn)行的操作
Push Pop
大家都知道 push 和 pop 操作是有導(dǎo)航控制器來進(jìn)行控制的冗懦,所以這里我們講一個(gè)在 push 界面的時(shí)候?yàn)橄乱患壍膶?dǎo)航控制器進(jìn)行添加返回按鈕和對 tabbar 的隱藏。
1.0 創(chuàng)建 TabbarController 自定義 NavigationController仇祭,設(shè)置自定義的 NavigationController 為 TabBarController 的 NavigationController
[self setupChildViewController:[[KlqNavViewController alloc] initWithRootViewController:[[KlqOneViewController alloc] init]] title:@"test1" image:@"tabbar_home_icon" selectedImage:@"tabbar_home_select_icon"];
[self setupChildViewController:[[KlqNavViewController alloc] initWithRootViewController:[[KlqTwoViewController alloc] init]] title:@"test2" image:@"tabbar_counters_icon" selectedImage:@"tabbar_counters_select_icon"];
[self setupChildViewController:[[KlqNavViewController alloc] initWithRootViewController:[[KlqThreeViewController alloc] init]] title:@"test4" image:@"tabbar_instr_icon" selectedImage:@"tabbar_instu_select_icon"];
[self setupChildViewController:[[KlqNavViewController alloc] initWithRootViewController:[[KlqFourViewController alloc] init]] title:@"test5" image:@"tabbar_mine_icon" selectedImage:@"tabbar_mine_select_icon"];
在 導(dǎo)航控制器進(jìn)行 push 操作的時(shí)候
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count > 0) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:YES];//這里進(jìn)行下簡單的說明披蕉,放到后面和放到前面是一樣子的,但是要注意的是如果放到前面的話乌奇,if 判斷中的 self.choildViewControllers 就要大于1才行了没讲。
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
return [super popViewControllerAnimated:YES];
}
通過這兩個(gè)方法可以攔截到在自定義的 navigationController 導(dǎo)航控制器控制的所有的 viewControlelr 發(fā)生的 push 和 pop 方法,這時(shí)候就可以在這里面對返回按鈕或者自定義push 動(dòng)畫進(jìn)行操作礁苗。
Present Dismiss
說完了 push 和 pop 操作食零,我們這時(shí)候在說下如果獲取到全局的 present 和 dismiss 操作,首先要了解的是我們的 present 和 dismiss 操作是不受導(dǎo)航控制器來進(jìn)行控制的寂屏。因此我們不能夠在導(dǎo)航控制器中寫 present 和 dismiss 操作贰谣,我們用到的方法是 hook 編程,即用到 objective 中的Method Swizzling 來獲取到 present 和 dismiss 方法迁霎,對它們進(jìn)行轉(zhuǎn)換吱抚,從而實(shí)現(xiàn)我們的需求。
Method Swizzling 方法的原理考廉,根本原理就是在程序運(yùn)行期間利用 IMP 和 SEL 動(dòng)態(tài)的給兩個(gè)方法進(jìn)行互換秘豹。
- IMP -> 就是指向一個(gè)方法實(shí)現(xiàn)的指針,大家要知道 OC 中方法在編譯期間會(huì)根據(jù) runtime 中的操作將方法保存到內(nèi)存中昌粤,因此我們調(diào)用方法的時(shí)候既绕,就需要根據(jù)方法的指針來對方法進(jìn)行調(diào)用。
- SEL -> 我們可以看到 objc/runtime.h 文件中的 objc_method 結(jié)構(gòu)體中是這么寫的涮坐,方法調(diào)用的時(shí)候會(huì)通過SEL找到 objc_method 結(jié)構(gòu)體凄贩,從而找到方法的 IMP指針。
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
}
下面我們看代碼:這里我們展示的是獲取到 present 的操作然后自定義下 present 的動(dòng)畫袱讹。
1. 我們這邊定義了一個(gè) ViewController 的 Category 來進(jìn)行對 ViewController 的擴(kuò)展疲扎。從而達(dá)到對 ViewController 的所有的方法的擴(kuò)展。
2. 在 viewController Category 中重寫 load 方法
+ (void)load{
[super load];
Method fromMethod = class_getInstanceMethod([self class], @selector(presentViewController:animated:completion:));
Method toMethod = class_getInstanceMethod([self class], @selector(swizzlingpresentViewController:animated:completion:));
if (!class_addMethod([self class], @selector(viewDidLoad), method_getImplementation(toMethod), method_getTypeEncoding(toMethod))) {
method_exchangeImplementations(fromMethod, toMethod);
}
}
3.重定義的 present 方法
- (void)swizzlingpresentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
//攔截到用戶的所有的 present 然后對視圖等信息進(jìn)行修改,調(diào)整椒丧。
viewControllerToPresent.transitioningDelegate = self;
[self swizzlingpresentViewController:viewControllerToPresent animated:YES completion:nil];
}
4.自定義模態(tài)方法 ShowMBTransitionAnimation
#pragma mark - TransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
ShowMBTransitionAnimation *showMBTransition = [ShowMBTransitionAnimation transitionWithType:ShowMBTransitionAnimationPresentType];
showMBTransition.toViewHeight = 500;
return showMBTransition;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
ShowMBTransitionAnimation *dismissMBTransition = [ShowMBTransitionAnimation transitionWithType:ShowMBTransitionAnimationDismissType];
dismissMBTransition.toViewHeight = 500;
return dismissMBTransition;
}