思路
自定義tabbar ,重新對按鈕進(jìn)行布局
CJTabbar * tabbar = [[CJTabbar alloc]init];
[self setValue:tabbar forKey:@"tabBar"];
// 創(chuàng)建中心按鈕
-(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);
}
// 處理凸出部分點(diǎn)擊事件不響應(yīng)的問題
-(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的底層實(shí)現(xiàn):
//
// 1.先看自己是否能接受觸摸事件
// 2.再看觸摸點(diǎn)是否在自己身上
// 3.從后往前遍歷子控件,拿到子控件后树碱,再次重復(fù)1,2步驟,要把父控件上的坐標(biāo)點(diǎn)轉(zhuǎn)換為子控件坐標(biāo)系下的點(diǎn)杜耙,再次執(zhí)行hitTest方法
// 4.若是最后還沒有找到合適的view,那么就return self拂盯,自己就是合適的view
//
//備注:當(dāng)控件接收到觸摸事件的時候佑女,不管能不能處理事件,都會調(diào)用hitTest方法
hitTest的底層實(shí)現(xiàn) 參考http://www.reibang.com/p/12af9103be03
tabBarItem 點(diǎn)擊圖片做了動畫處理
-(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"];
//速度控制函數(shù)谈竿,控制動畫運(yùn)行的節(jié)奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2; //執(zhí)行時間
animation.repeatCount = 1; //執(zhí)行次數(shù)
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; //保證動畫效果延續(xù)
animation.fromValue = [NSNumber numberWithFloat:1.0]; //初始伸縮倍數(shù)
animation.toValue = [NSNumber numberWithFloat:1.15]; //結(jié)束伸縮倍數(shù)
[[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圖標(biāo)點(diǎn)擊動畫效果參考 http://www.reibang.com/p/b1d277a3d148
tabbar中間凸起团驱,Tabbar item圖標(biāo)點(diǎn)擊動畫效果完整代碼GitHub