美文网首页
2018-12-26 自定义TabBar点击动画

2018-12-26 自定义TabBar点击动画

作者: zxh123456 | 来源:发表于2018-12-26 17:06 被阅读13次

    效果如下:


    fdabcf8d6aaf94559c701393b7c7d1aa.GIF

    首页点击2次执行刷新,其他点1次有个缩放动画

    主要代码:

    
    - (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item{
    
         //消除上次首页点击刷新动画
    
         [self.animationImageV.layer removeAllAnimations];
    
         [self.animationImageV removeFromSuperview];
    
         // 判断本次点击的UITabBarItem是否和上次的一样
    
         if (item == self.lastItem && [CommParms shareInstance].user) { // 一样就发出通知
    
              [[NSNotificationCenter defaultCenter] postNotificationName:TabbarDidClickedNotification object:nil userInfo:nil];
    
         }
    
         NSInteger index = [self.tabBar.items indexOfObject:item];
    
         if(item ==self.lastItem&& index ==0) {//连续点击首页2次,
    
              //执行首页刷新动画
    
              [self homeRefreshAnimation];
    
         }else{//其他情况
    
              //简单的缩放动画
    
              [selfanimationWithIndex:index];
    
         }
    
         // 将这次点击的UITabBarItem赋值给属性
    
         self.lastItem= item;
    
    }
    
    
    
    // 首页刷新动画
    
    - (void)homeRefreshAnimation{
    
         NSMutableArray * tabbarbuttonArray = [NSMutableArray array];//创建btn数组
    
         NSMutableArray* tabbarbuttonImgArray = [NSMutableArrayarray];//创建img数组
    
         for(UIView*tabBarButtoninself.tabBar.subviews) {
    
              if([tabBarButtonisKindOfClass:NSClassFromString(@"UITabBarButton")]) {
    
                   [tabbarbuttonArrayaddObject:tabBarButton];//添加到btn数组
    
                   for(UIView*imageViewintabBarButton.subviews) {
    
                        if([imageViewisKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
    
                             [tabbarbuttonImgArrayaddObject:imageView];//添加到img数组
    
                        }
    
                   }
    
              }
    
         }
    
         UIView*firstTabBarBtn = tabbarbuttonArray[0];//第一个tabBarBtn
    
         UIView*firstTabBarImgV = tabbarbuttonImgArray[0];//第一个图片
    
         UIImageView*animationImageV = [[UIImageViewalloc]initWithFrame:firstTabBarImgV.frame];
    
         self.animationImageV= animationImageV;
    
         self.firstTabBarImgV= firstTabBarImgV;
    
         animationImageV.image= [UIImageimageNamed:@"icon_TaBarRefresh"];
    
         [firstTabBarBtnaddSubview:animationImageV];
    
         firstTabBarImgV.hidden=YES;
    
         CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
         rotation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
         rotation.duration=0.88;
    
         rotation.repeatCount=1;
    
         rotation.autoreverses=NO;
    
         rotation.fromValue= [NSNumber numberWithFloat:0];
    
         rotation.toValue= [NSNumber numberWithFloat:M_PI * 2];
    
         rotation.delegate=self;
    
         [animationImageV.layeraddAnimation:rotationforKey:nil];
    
    }
    
    
    
    // 简单的缩放动画
    
    - (void)animationWithIndex:(NSInteger) index {
    
         NSMutableArray * tabbarbuttonArray = [NSMutableArray array];//创建btn数组
    
         for(UIView*tabBarButtoninself.tabBar.subviews) {
    
              if([tabBarButtonisKindOfClass:NSClassFromString(@"UITabBarButton")]) {
    
                   [tabbarbuttonArrayaddObject:tabBarButton];//添加到btn数组
    
              }
    
         }
    
         CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
         pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
         pulse.duration=0.12;
    
         pulse.repeatCount=1;
    
         pulse.autoreverses=YES;
    
         pulse.fromValue= [NSNumber numberWithFloat:0.7];
    
         pulse.toValue= [NSNumber numberWithFloat:1.3];
    
         [[tabbarbuttonArray[index]layer]addAnimation:pulseforKey:nil];
    
    }
    
    
    
    #pragma mark -----CAAnimationDelegate
    
    - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag{
    
         [_animationImageV removeFromSuperview];
    
         _firstTabBarImgV.hidden = NO;
    
    }
    
    

    相关文章

      网友评论

          本文标题:2018-12-26 自定义TabBar点击动画

          本文链接:https://www.haomeiwen.com/subject/ddxklqtx.html