- 摘要
實(shí)現(xiàn)同iOS 11 App Store Today相似的轉(zhuǎn)場效果.本文嘗試解決轉(zhuǎn)場(Transition)過程中TabBar的隱藏(向下滑出屏幕).
遇到的問題
項(xiàng)目中使用的是系統(tǒng)自帶的Storyboard上的TabBar,TabBar的隱藏使用的是hidesBottomBarWhenPushed
方法.實(shí)現(xiàn)自定義轉(zhuǎn)場的過程中,TabBar會跟隨轉(zhuǎn)場動畫向左移出屏幕,但需求是向下滑出屏幕.如果自定義TabBar可能更容易實(shí)現(xiàn),具體效果可以點(diǎn)擊App Store上的"今日 App".
解決的問題
僅在自定義轉(zhuǎn)場中按需隱藏TabBar(push向下滑出屏幕,pop向上劃入屏幕),不影響其他使用系統(tǒng)轉(zhuǎn)場的頁面.
解決方案
- 使用方法
首先確定是否使用的自定義轉(zhuǎn)場,如果定義了自定義轉(zhuǎn)場的類,在要使用的頁面實(shí)現(xiàn)UINavigationControllerDelegate
的代理方法
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
//根據(jù)類型返回對應(yīng)動畫
if (operation == UINavigationControllerOperationPush) {
return _trans;
}else {
return nil;
}
}
在我的工程中,存在著重寫的TabBar
、UITabBarController
恤浪、UINavigationController
的類分別實(shí)現(xiàn)不同的需求,要實(shí)現(xiàn)這種新增加的效果,最好的方法應(yīng)該是使用Category
,所以我們定義UITabBar
的類別UITabBar+CustomTabbar
,實(shí)現(xiàn)動畫(出處作者稱為淡入淡出效果).
UITabBar+CustomTabbar.h
#import <UIKit/UIKit.h>
@interface UITabBar (HYCustomTabBar)<CAAction>
@end
UITabBar+CustomTabbar.m
#import "UITabBar+CustomTabbar.h"
@implementation UITabBar (CustomTabbar)
-(id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event{
if ([event isEqualToString:@"position"]) {
if(layer.position.x<0){
//show tabbar
CATransition *pushFromTop = [[CATransition alloc] init];
pushFromTop.duration = 0.3;
pushFromTop.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
pushFromTop.type = kCATransitionPush;
pushFromTop.subtype = kCATransitionFromTop;
return pushFromTop;
}else if (layer.position.x>0&&(layer.position.y>layer.bounds.size.height)&&(layer.position.y<[UIScreen mainScreen].bounds.size.height)){
//hide tabbar
CATransition *pushFromBottom = [[CATransition alloc] init];
pushFromBottom.duration = 0.3;
pushFromBottom.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
pushFromBottom.type = kCATransitionPush;
pushFromBottom.subtype = kCATransitionFromBottom;
return pushFromBottom;
}
}
return (id<CAAction>)[NSNull null];
}
-(void)runActionForKey:(NSString *)event object:(id)anObject arguments:(NSDictionary *)dict{
}
@end
其中
layer.position.y>layer.bounds.size.height
layer.position.y<[UIScreen mainScreen].bounds.size.height
是為了去除第一次加載視圖的情況僚饭。
然后在自己重寫的UITabBarController
類中添加類別的引用
#import "TabVC.h"
#import "UITabBar+CustomTabbar.h"
@interface TabVC ()
@end
...
- 這樣,在自定義轉(zhuǎn)場過程中,就能實(shí)現(xiàn)自定義的TabBar隱藏動效,而且實(shí)現(xiàn)了很大程度的解耦.