UITabBarController added lottie animation

The principle is to traverse a imageView, doing animation, add animation view, after the end of the animation hidden view
only tested iOS11 ~ 13, if you have questions, please contact me.

github effective and demo

interface

@interface CDKTabBarController ()<UITabBarControllerDelegate>

/// 关联到 controller 原因:解决快速点击两个不一样的 tabbar 后,需要关闭第一次点击的动画
@property(nonatomic, strong) LOTAnimationView *animationView;

@end

implementation

@implementation CDKTabBarController


#pragma mark - Animation
- (LOTAnimationView *)getAnimationViewAtTabbarIndex:(NSInteger)index frame:(CGRect)frame {
    
    // tabbar1 。。。 tabbar3
    LOTAnimationView *view = [LOTAnimationView animationNamed:[NSString stringWithFormat:@"tabbar%ld",index+1]];
    view.frame = frame;
    view.contentMode = UIViewContentModeScaleAspectFill;
    view.animationSpeed = 1;
    return view;
}

- (void)setupAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
    
    if (self.animationView) {
        [self.animationView stop];
    }
    
    //1.
    NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    
    __block NSMutableArray <UIImageView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
    
    //2.
    for (UIView *tempView in tabBarController.tabBar.subviews) {
        
        if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")])
        {
            //2.1
            for (UIImageView *tempImageView in tempView.subviews) {
                if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
                    [tabBarSwappableImageViews addObject:tempImageView];
                }
            }
        }
    }
    
    //3.
    __block UIImageView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
    
    //4.
    CGRect frame = currentTabBarSwappableImageView.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    __block LOTAnimationView *animationView = [self getAnimationViewAtTabbarIndex:index frame:frame];
    self.animationView = animationView;
    animationView.center = currentTabBarSwappableImageView.center;
    [currentTabBarSwappableImageView.superview addSubview:animationView];
    
    currentTabBarSwappableImageView.hidden = YES;
    NSLog(@"%@",animationView);
    //6.
    [animationView playFromProgress:0 toProgress:1 withCompletion:^(BOOL animationFinished) {
        currentTabBarSwappableImageView.hidden = NO;
        [animationView removeFromSuperview];
        animationView = nil;
    }];
}

#pragma mark - UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    [self setupAnaimationWithTabBarController:tabBarController selectViewController:viewController];
}

Guess you like

Origin www.cnblogs.com/benxiaokang/p/uitabbarcontroller-jia-ru-lottie-dong-hua.html