UITabBarViewController动画切换

UIImage+Label

Three.gif
@interface OPTabBarViewController ()

@property (nonatomic, assign) NSUInteger oldSelectedIndex;

@property (nonatomic, strong) NSMutableArray *tabBarMutableArray;
@end

@implementation OPTabBarViewController
#pragma mark - 添加动画
- (NSMutableArray *)tabBarMutableArray
{
    if (_tabBarMutableArray == nil) {
        _tabBarMutableArray = [NSMutableArray array];
        
        for (UIView *view in self.tabBar.subviews) {
            if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [_tabBarMutableArray addObject:view];
                view.backgroundColor = KClearColor;
            }
        }
    }
    
    return _tabBarMutableArray;
}

#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSInteger selectIndex = [tabBar.items indexOfObject:item];
    
    [self animationTabBarWithIndex:selectIndex];
}

#pragma mark - setSelectedIndex
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    [self animationTabBarWithIndex:selectedIndex];
}

#pragma mark - 动画详情(UIImage+Label)
- (void)animationTabBarWithIndex:(NSUInteger)index
{
    if (index != self.oldSelectedIndex) {
        
        [[self.tabBarMutableArray[self.oldSelectedIndex] layer] removeAllAnimations];
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
        animation.keyPath = @"transform.scale";
        animation.values = @[@1.0,@1.15,@0.95,@1.15,@0.90,@1.10,@1.0];
        animation.duration = 1.0;
        animation.calculationMode = kCAAnimationCubic;
        
        [[self.tabBarMutableArray[index] layer] addAnimation:animation forKey:nil];
        
        self.oldSelectedIndex = index;
    }
}
@end

你可能感兴趣的:(UITabBarViewController动画切换)