iOS 实现tabbar点击的动画效果

5 篇文章 0 订阅

实现tabbar点击的帧动画效果, 可以用下面两种方法

方法1:

1.在UITabBarController 里面

 


@interface UITabBarController () <UITabBarControllerDelegate>
//注意数组是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;

@end

2

 

//UITabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.imagesArray = [NSMutableArray array];
    for (int i = 0; i<4; i++) {
        NSMutableArray *images = [NSMutableArray array];
        switch (i) {
            case 0:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"home.bundle/tabbar_home_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 1:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"center.bundle/tabbar_senter_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 2:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"class.bundle/tabbar_class_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 3:
                for (int j = 0; j<=13; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"me.bundle/tabbar_me_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            default:
                break;
        }
        [self.imagesArray addObject:images];
        
    }

3.实现 UITabBarControllerDelegate 方法

  • (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

 

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
   NSMutableArray *tabBarBtnArray = [NSMutableArray array];
   int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
   // 获取UITabBarButton
   for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
       UIView * tabBarButton = self.tabBar.subviews[i];
       if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
           [tabBarBtnArray addObject:tabBarButton];
           // NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
       }
   }
   //获取当前的UITabBarButton
   UIView *TabBarButton = tabBarBtnArray[index];
   NSArray *images = self.imagesArray[index];
   for (UIView *imageV in TabBarButton.subviews) {
       if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
           CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
           //animation.delegate = self;
           animation.values = images;
           animation.duration = 0.3;// images.count * 0.08;
           animation.calculationMode = kCAAnimationCubic;
           [imageV.layer addAnimation:animation forKey:nil];  
       }
   }
  
   
}

方法2 , 和方法一区别不大, 只需修改下面代码

 

//注意数组是UIImage不是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSMutableArray *tabBarBtnArray = [NSMutableArray array];
    int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
    // 获取UITabBarButton
    for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
        UIView * tabBarButton = self.tabBar.subviews[i];
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarBtnArray addObject:tabBarButton];
            // NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
        }
    }
    //获取当前的UITabBarButton
    UIView *TabBarButton = tabBarBtnArray[index];
    NSArray *images = self.imagesArray[index];
    for (UIView *imageV in TabBarButton.subviews) {
        if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
           
             //图片是是要UIImage的
             UIImageView *imageView = (UIImageView*)imageV;
             imageView.animationImages = images;
             imageView.animationDuration = images.count * 0.08;
             imageView.animationRepeatCount = 1;
             [imageView startAnimating];
        }
    }
      
}

3.另外你可以设置tabbar的其他属性

 

//1设置线条颜色(遮挡法)
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, SCREEN_WIDTH + 1, 0.5)];
    view.backgroundColor = RGBWithAlpha(0xe3e5e9, 1.0);
    [[UITabBar appearance] insertSubview:view atIndex:0];
//1.tabbar标题颜色
    NSDictionary *attributes_normol = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xADADAD, 1)};
    
    NSDictionary *attributes_sel = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xFF5847, 1)};
    
    for (UITabBarItem *item in self.tabBar.items) {
       // item.imageInsets = UIEdgeInsetsMake(-3, 0, 0, 0);
        //item.titlePositionAdjustment = UIOffsetMake(0, -4);
        [item setTitleTextAttributes:attributes_normol forState:UIControlStateNormal];
        [item setTitleTextAttributes:attributes_sel forState:UIControlStateSelected];
    }
//假如tabbar的图片大小不合适, 不想换图的话可以重新生成一张图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{
    UIGraphicsBeginImageContextWithOptions(newSize, 0, [UIScreen mainScreen].scale);
    //UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}



作者:吴纪磊
链接:https://www.jianshu.com/p/d8b4bcfd4ae6
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Flutter 中,你可以使用 `CupertinoTabBar` 和 `CupertinoTabScaffold` 来实现带有动画TabBar 切换效果。下面是一个简单的示例代码: ```dart import 'package:flutter/cupertino.dart'; class MyTabScreen extends StatefulWidget { @override _MyTabScreenState createState() => _MyTabScreenState(); } class _MyTabScreenState extends State<MyTabScreen> { int _currentIndex = 0; @override Widget build(BuildContext context) { return CupertinoTabScaffold( tabBar: CupertinoTabBar( items: [ BottomNavigationBarItem( icon: Icon(CupertinoIcons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.search), label: 'Search', ), BottomNavigationBarItem( icon: Icon(CupertinoIcons.person), label: 'Profile', ), ], currentIndex: _currentIndex, onTap: (index) { setState(() { _currentIndex = index; }); }, ), tabBuilder: (BuildContext context, int index) { return CupertinoTabView( builder: (BuildContext context) { switch (index) { case 0: return HomeScreen(); case 1: return SearchScreen(); case 2: return ProfileScreen(); default: return Container(); } }, ); }, ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Home'), ), child: Center( child: Text('Home Screen'), ), ); } } class SearchScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Search'), ), child: Center( child: Text('Search Screen'), ), ); } } class ProfileScreen extends StatelessWidget { @override Widget build(BuildContext context) { return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( middle: Text('Profile'), ), child: Center( child: Text('Profile Screen'), ), ); } } ``` 在这个示例中,我们使用 `CupertinoTabScaffold` 和 `CupertinoTabBar` 来创建底部的 TabBar,然后在 `tabBuilder` 方法中根据索引值切换显示不同的页面。在 `onTap` 回调中,我们可以更新 `_currentIndex` 的值来实现切换 Tab 时的动画效果。 以上是一个基本的示例,你可以根据自己的需求进行定制和修改。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值