我參照網(wǎng)上的說法
- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item{
? ? NSLog(@"%zi",self.selectedIndex);
? ? NSIntegerindex = [self.tabBar.itemsindexOfObject:item];
? ? if(self.indexFlag!= index) {
? ? ? ? if(!_btArray.count) {
? ? ? ? ? ? NSMutableDictionary *dic = [NSMutableDictionary new];
? ? ? ? ? ? for(UIView*btninself.tabBar.subviews) {
? ? ? ? ? ? ? ? if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
? ? ? ? ? ? ? ? ? ? [_btArray addObject btn];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //放大效果,并回到原位
? ? ? ? CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
? ? ? ? //速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏
? ? ? ? animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
? ? ? ? animation.duration=0.08;? ? ? //執(zhí)行時(shí)間
? ? ? ? animation.repeatCount=1;? ? ? //執(zhí)行次數(shù)
? ? ? ? animation.autoreverses=YES;? ? //完成動(dòng)畫后會(huì)回到執(zhí)行動(dòng)畫之前的狀態(tài)
? ? ? ? animation.fromValue= [NSNumbernumberWithFloat:0.7];? //初始伸縮倍數(shù)
? ? ? ? animation.toValue= [NSNumbernumberWithFloat:1.3];? ? //結(jié)束伸縮倍數(shù)
? ? ? ? [[_btArray[index]layer]addAnimation:animationforKey:nil];
? ? ? ? self.indexFlag= index;
? ? }
}
發(fā)現(xiàn)點(diǎn)擊的時(shí)候,根本不是當(dāng)前按鈕,打印了,發(fā)現(xiàn)
UITabBarButton: 0x105cb7dd0; frame = (252 1; 121 48); opaque = NO;
UITabBarButton: 0x105cb3ba0; frame = (127 1; 121 48);opaque = NO;
UITabBarButton: 0x10c512830; frame = (2 1; 121 48); opaque = NO;?
這個(gè)按鈕返回的順序發(fā)生變化,這個(gè)就需要重新排序一下,就可以了
if (!_btArray.count) {
? ? ? ? ? ? NSMutableDictionary *dic = [NSMutableDictionary new];
? ? ? ? ? ? for(UIView*btninself.tabBar.subviews) {
? ? ? ? ? ? ? ? if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
? ? ? ? ? ? ? ? ? ? dic[@(btn.frame.origin.x)] = btn;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? NSArray*array = dic.allKeys;
? ? ? ? ? ? array = [arraysortedArrayUsingSelector:@selector(compare:)];
? ? ? ? ? ? @weakify(self)
? ? ? ? ? ? [arrayenumerateObjectsUsingBlock:^(id? _Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
? ? ? ? ? ? ? ? @strongify(self)
? ? ? ? ? ? ? ? UIView*bt = dic[obj];
? ? ? ? ? ? ? ? [self.btArrayaddObject:bt];
? ? ? ? ? ? }];
? ? ? ? }
? ? ? ? //放大效果,并回到原位
? ? ? ? CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
? ? ? ? //速度控制函數(shù),控制動(dòng)畫運(yùn)行的節(jié)奏
? ? ? ? animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
? ? ? ? animation.duration=0.08;? ? ? //執(zhí)行時(shí)間
? ? ? ? animation.repeatCount=1;? ? ? //執(zhí)行次數(shù)
? ? ? ? animation.autoreverses=YES;? ? //完成動(dòng)畫后會(huì)回到執(zhí)行動(dòng)畫之前的狀態(tài)
? ? ? ? animation.fromValue= [NSNumbernumberWithFloat:0.7];? //初始伸縮倍數(shù)
? ? ? ? animation.toValue= [NSNumbernumberWithFloat:1.3];? ? //結(jié)束伸縮倍數(shù)
? ? ? ? [[_btArray[index]layer]addAnimation:animationforKey:nil];
? ? ? ? self.indexFlag= index;
這樣就可以了,無論你怎么點(diǎn)擊,都是對(duì)了。必須要注意,返回按鈕的區(qū)域的順序問題。