Newer
Older
XinYang_IOS / BaseViewController / YHBaseNavigationController.m
@zhangfeng zhangfeng on 7 Dec 8 KB 1.8.0
//
//  YHBaseNavigationController.m
//  YHScrollView_Test
//
//  Created by Jim on 2021/9/4.
//

#import "YHBaseNavigationController.h"
#import <objc/runtime.h>
#import "UIImage+Category.h"

#define DURATION        0.5f

@interface YHBaseNavigationController ()
@property (nonatomic,assign)BOOL currentAnimating;
@end

@implementation YHBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationBar setBackgroundColor:kColorNaviTintColor];
    self.navigationBar.tintColor= kColorNaviTintColor;
    [self.navigationBar setBarTintColor:kColorNaviTintColor];
    [self.navigationBar setShadowImage:[UIImage drawImageWithSize:CGSizeMake(self.navigationBar.frame.size.width, self.navigationBar.frame.size.height) color:[UIColor clearColor]]];
    self.interactivePopGestureRecognizer.delegate = self;
   
    NSDictionary * dict = @{NSForegroundColorAttributeName:kColorNavTitleColor,
                            NSFontAttributeName:[UIFont systemFontOfSize:16]
    };
    self.navigationBar.titleTextAttributes = dict;
    self.delegate = self;
    
    self.bottomView=[UIImageView new];
    self.bottomView.frame=CGRectMake(0, KNavHeight-KStatusBarHeight, KScreenWidth, 9);
    self.bottomView.image=[UIImage streImageNamed:@"navi_back"];
    [self.navigationBar addSubview:self.bottomView];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}

+ (void)pushViewController:(UIViewController *)viewController hiddenBottomWhenPush:(BOOL)hiddenBottomWhenPush animation:(BOOL)animation fromNavigation:(UINavigationController *)navi
{
    
    viewController.hidesBottomBarWhenPushed = hiddenBottomWhenPush;
    [navi pushViewController:viewController
                    animated:animation];
}


-(void)hiddenBottomView:(BOOL)hidden{
    self.bottomView.hidden=hidden;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if (self.viewControllers.count > 1) {
        return YES;
    }
    return NO;
}

#pragma mark Transition动画
- (void)Fish_navigationTransitionWithType:(AnimationType)type subtype:(AnimationSubtype) subtype{
    [self.view.layer removeAnimationForKey:@"animation"];
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = DURATION;
    //设置运动type
    animation.type = [self animationType:type];
    //设置子类
//    animation.subtype = [self animtionSubtype:subtype];
    //设置运动速度
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.view.layer addAnimation:animation forKey:@"animation"];
    
}

- (NSString *)animationType:(NSInteger)type{
    NSString *animationType = kCATransitionFade;
    switch (type) {
        case AnimationTypeFade:
            animationType = kCATransitionFade;
            break;
    
        case AnimationTypePush:
            animationType = kCATransitionPush;
            break;
            
        case AnimationTypeReveal:
            animationType = kCATransitionReveal;
            break;
        case AnimationTypeMoveIn:
            animationType = kCATransitionMoveIn;
            break;
            
        case AnimationTypeCube:
            animationType = @"cube";
            break;
            
        case AnimationTypeSuckEffect:
            animationType = @"suckEffect";
            break;
            
        case AnimationTypeOglFlip:
            animationType = @"oglFlip";
            break;
            
        case AnimationTypeRippleEffect:
            animationType = @"rippleEffect";
            break;
            
        case AnimationTypePageCurl:
            animationType = @"pageCurl";
            break;
            
        case AnimationTypePageUnCurl:
            animationType = @"pageUnCurl";
            break;
            
        case AnimationTypeCameraIrisHollowOpen:
            animationType = @"cameraIrisHollowOpen";
            break;
            
        case AnimationTypeCameraIrisHollowClose:
            animationType = @"cameraIrisHollowClose";
            break;
    }
    return animationType;
    
}
- (NSString *)animtionSubtype:(NSInteger)subtype{
    
    NSString *subtypeString = kCATransitionFromLeft;
    switch (subtype) {
        case AnimationSubtypeLeft:
            subtypeString = kCATransitionFromLeft;
            break;
        case AnimationSubtypeBottom:
            subtypeString = kCATransitionFromBottom;
            break;
        case AnimationSubtypeRight:
            subtypeString = kCATransitionFromRight;
            break;
        case AnimationSubtypeTop:
            subtypeString = kCATransitionFromTop;
            break;
        default:
            break;
    }
    return subtypeString;
    
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (_currentAnimating) {
        return;
    }else if (animated){
        _currentAnimating = YES;
    }
    //滑动过程中你会发现如果在pushViewController的动画过程中激活滑动手势会导致crash, 解决方案:
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
    if(self.viewControllers.count > 1){
//        UIButton* btn = [[UIButton alloc]init];
//        btn.frame = CGRectMake(0, 0, 50, 40);
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -45, 0, 0 );
//        [btn setImage:[Scorpionweed findImageWith:@"arrow_left_white"] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(Fish_goBack) forControlEvents:UIControlEventTouchUpInside];
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"login_goBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
            style:UIBarButtonItemStyleDone target:self action:@selector(Fish_goBack)];
    }
    
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    
    if (_currentAnimating) {
        return nil;
    }else if (animated){
        _currentAnimating = YES;
    }
    return [super popViewControllerAnimated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    _currentAnimating = NO;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    [[self transitionCoordinator] notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context){
         
         if([context isCancelled]){
             
             UIViewController *fromViewController = [context viewControllerForKey:UITransitionContextFromViewControllerKey];
             
             [self navigationController:navigationController willShowViewController:fromViewController animated:animated];
             
             
             if([self respondsToSelector:@selector(navigationController:didShowViewController:animated:)]){
                 
                 NSTimeInterval animationCompletion = [context transitionDuration] * [context percentComplete];
                 
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (uint64_t)animationCompletion * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                     
                     [self navigationController:navigationController didShowViewController:fromViewController animated:animated];
                     
                 });
                 
             }
             
         }
         
     }];
    
}

- (void)Fish_goBack {
    if ([UIDevice currentDevice].systemVersion.doubleValue == 12.2) {//避免ios 12.2返回有动画时大厅头部信息布局错乱
        [self popViewControllerAnimated:NO];
    }else{
        [self popViewControllerAnimated:YES];
    }
}

@end