ios上UITabbar上的按鈕的跳動(dòng)動(dòng)畫

我參照網(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ū)域的順序問題。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容