iOS tabbar中间凸起,Tabbar item图标点击动画效果

思路

自定义tabbar ,重新对按钮进行布局

    CJTabbar * tabbar = [[CJTabbar alloc]init];
    [self setValue:tabbar forKey:@"tabBar"];
//  创建中心按钮
-(void)creatCenterButton{
    
    UIButton * centerButton = [UIButton buttonWithType:0];
    self.centerButton = centerButton;
    [centerButton addTarget:self action:@selector(centerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [centerButton setImage:[UIImage imageNamed:@"money"] forState:UIControlStateNormal];
    [self addSubview:centerButton];
    
}
//  重新布局
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat buttonW = self.bounds.size.width / 5;
    NSInteger index = 0;
    for (NSInteger i = 0;  i < self.subviews.count; i++) {
            UIView * tabbarButton = self.subviews[i];
            if ([tabbarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {

                if (index == 2) {
                       index++;
                }
                tabbarButton.frame = CGRectMake(index*buttonW, 0, buttonW, 49);
                index++;
            }
    }
    self.centerButton.frame =  CGRectMake(2*buttonW, -20, buttonW, 49);
}
//  处理凸出部分点击事件不响应的问题
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    CGPoint tempPoint = [self convertPoint:point toView:self.centerButton];
    if ([self.centerButton pointInside:tempPoint withEvent:event]) {
        return self.centerButton;
    }else{
        return [super hitTest:point withEvent:event];
    }
}

注:

//hitTest的底层实现:
//
//    1.先看自己是否能接受触摸事件
//    2.再看触摸点是否在自己身上
//    3.从后往前遍历子控件,拿到子控件后,再次重复1,2步骤,要把父控件上的坐标点转换为子控件坐标系下的点,再次执行hitTest方法
//    4.若是最后还没有找到合适的view,那么就return self,自己就是合适的view
//
//备注:当控件接收到触摸事件的时候,不管能不能处理事件,都会调用hitTest方法

hitTest的底层实现 参考//www.greatytc.com/p/12af9103be03

tabBarItem 点击图片做了动画处理

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
        NSInteger index = [self.tabBar.items indexOfObject:item];
         if (index != self.itemSelectIndex) {
               //获取按钮
               NSMutableArray *arry = [NSMutableArray array];
               for (UIView *btn in self.tabBar.subviews) {
                   if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                        [arry addObject:btn];
                   }
               }
                //添加动画
                 //放大效果
                 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
                 //速度控制函数,控制动画运行的节奏
                 animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                 animation.duration = 0.2;       //执行时间
                 animation.repeatCount = 1;      //执行次数
                 animation.removedOnCompletion = NO;
                 animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
                 animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
                 animation.toValue = [NSNumber numberWithFloat:1.15];     //结束伸缩倍数
                 [[arry[index] layer] addAnimation:animation forKey:nil];
                 //移除其他tabbar的动画
                 for (int i = 0; i<arry.count; i++) {
                     if (i != index) {
                         [[arry[i] layer] removeAllAnimations];
                     }
                 }
                 self.itemSelectIndex = index;
             }

}

Tabbar图标点击动画效果参考 //www.greatytc.com/p/b1d277a3d148
tabbar中间凸起,Tabbar item图标点击动画效果完整代码GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容