TabBarItem切换动画

来源:互联网 发布:最大公约数c语言程序 编辑:程序博客网 时间:2024/06/05 12:04

为工程的底部工具栏添加一个简单而小萌的动画,原理是在 UITabBarController的里面切换Item时的方法中添加一个缩放动画。

实现的效果如下GIF图,




代码如下,

//tabbar选中item

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

{

    [selfanimationWithIndex:[self.tabBar.itemsindexOfObject:item]];

}


#pragma mark - TabBar Item选中时的动画

- (void)animationWithIndex:(NSInteger) index {

    

    NSMutableArray * tabBarButtonArray = [NSMutableArrayarray];

    for (UIView *tabBarButtoninself.tabBar.subviews) {

        if ([tabBarButtonisKindOfClass:NSClassFromString(@"UITabBarButton")]) {

            [tabBarButtonArray addObject:tabBarButton];

        }

    }

    CABasicAnimation * animation = [CABasicAnimationanimationWithKeyPath:@"transform.scale"];

    animation.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    animation.duration =0.1;

    animation.repeatCount =1;

    animation.autoreverses =YES;

    animation.fromValue =@0.6;

    animation.toValue =@1.4;

    [[tabBarButtonArray[index] layer]addAnimation:animationforKey:nil];

}



0 0