iOS 實(shí)現(xiàn)tabbar點(diǎn)擊的動(dòng)畫效果(幀動(dòng)畫)

實(shí)現(xiàn)tabbar點(diǎn)擊的幀動(dòng)畫效果, 可以用下面兩種方法


Jietu20190722-103120-HD.gif

方法1:

1.在UITabBarController 里面


@interface UITabBarController () <UITabBarControllerDelegate>
//注意數(shù)組是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;

@end

2

//UITabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.imagesArray = [NSMutableArray array];
    for (int i = 0; i<4; i++) {
        NSMutableArray *images = [NSMutableArray array];
        switch (i) {
            case 0:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"home.bundle/tabbar_home_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 1:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"center.bundle/tabbar_senter_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 2:
                for (int j = 0; j<=10; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"class.bundle/tabbar_class_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            case 3:
                for (int j = 0; j<=13; j++) {
                    NSString *imageName = [NSString stringWithFormat:@"me.bundle/tabbar_me_sel_%d",j];
                    [images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
                }
                break;
            default:
                break;
        }
        [self.imagesArray addObject:images];
        
    }

3.實(shí)現(xiàn) UITabBarControllerDelegate 方法

  • (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
   NSMutableArray *tabBarBtnArray = [NSMutableArray array];
   int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
   // 獲取UITabBarButton
   for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
       UIView * tabBarButton = self.tabBar.subviews[i];
       if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
           [tabBarBtnArray addObject:tabBarButton];
           // NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
       }
   }
   //獲取當(dāng)前的UITabBarButton
   UIView *TabBarButton = tabBarBtnArray[index];
   NSArray *images = self.imagesArray[index];
   for (UIView *imageV in TabBarButton.subviews) {
       if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
           CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
           //animation.delegate = self;
           animation.values = images;
           animation.duration = 0.3;// images.count * 0.08;
           animation.calculationMode = kCAAnimationCubic;
           [imageV.layer addAnimation:animation forKey:nil];  
       }
   }
  
   
}

方法2 , 和方法一區(qū)別不大, 只需修改下面代碼

//注意數(shù)組是UIImage不是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSMutableArray *tabBarBtnArray = [NSMutableArray array];
    int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
    // 獲取UITabBarButton
    for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
        UIView * tabBarButton = self.tabBar.subviews[i];
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBarBtnArray addObject:tabBarButton];
            // NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
        }
    }
    //獲取當(dāng)前的UITabBarButton
    UIView *TabBarButton = tabBarBtnArray[index];
    NSArray *images = self.imagesArray[index];
    for (UIView *imageV in TabBarButton.subviews) {
        if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
           
             //圖片是是要UIImage的
             UIImageView *imageView = (UIImageView*)imageV;
             imageView.animationImages = images;
             imageView.animationDuration = images.count * 0.08;
             imageView.animationRepeatCount = 1;
             [imageView startAnimating];
        }
    }
      
}

3.另外你可以設(shè)置tabbar的其他屬性

//1設(shè)置線條顏色(遮擋法)
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, SCREEN_WIDTH + 1, 0.5)];
    view.backgroundColor = RGBWithAlpha(0xe3e5e9, 1.0);
    [[UITabBar appearance] insertSubview:view atIndex:0];
//1.tabbar標(biāo)題顏色
    NSDictionary *attributes_normol = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xADADAD, 1)};
    
    NSDictionary *attributes_sel = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xFF5847, 1)};
    
    for (UITabBarItem *item in self.tabBar.items) {
       // item.imageInsets = UIEdgeInsetsMake(-3, 0, 0, 0);
        //item.titlePositionAdjustment = UIOffsetMake(0, -4);
        [item setTitleTextAttributes:attributes_normol forState:UIControlStateNormal];
        [item setTitleTextAttributes:attributes_sel forState:UIControlStateSelected];
    }
//假如tabbar的圖片大小不合適, 不想換圖的話可以重新生成一張圖片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

{
    UIGraphicsBeginImageContextWithOptions(newSize, 0, [UIScreen mainScreen].scale);
    //UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迟螺,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子啤月,更是在濱河造成了極大的恐慌煮仇,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谎仲,死亡現(xiàn)場(chǎng)離奇詭異浙垫,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門夹姥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來杉武,“玉大人,你說我怎么就攤上這事辙售∏岜В” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵旦部,是天一觀的道長(zhǎng)祈搜。 經(jīng)常有香客問我,道長(zhǎng)士八,這世上最難降的妖魔是什么容燕? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮婚度,結(jié)果婚禮上蘸秘,老公的妹妹穿的比我還像新娘。我一直安慰自己蝗茁,他們只是感情好醋虏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著哮翘,像睡著了一般颈嚼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上饭寺,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天粘舟,我揣著相機(jī)與錄音,去河邊找鬼佩研。 笑死,一個(gè)胖子當(dāng)著我的面吹牛霞揉,可吹牛的內(nèi)容都是我干的旬薯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼适秩,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼绊序!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起秽荞,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤骤公,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后扬跋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阶捆,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了洒试。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片倍奢。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖垒棋,靈堂內(nèi)的尸體忽然破棺而出卒煞,到底是詐尸還是另有隱情,我是刑警寧澤叼架,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布畔裕,位于F島的核電站,受9級(jí)特大地震影響乖订,放射性物質(zhì)發(fā)生泄漏扮饶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一垢粮、第九天 我趴在偏房一處隱蔽的房頂上張望贴届。 院中可真熱鬧,春花似錦蜡吧、人聲如沸毫蚓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽元潘。三九已至,卻和暖如春君仆,著一層夾襖步出監(jiān)牢的瞬間翩概,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工返咱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钥庇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓咖摹,卻偏偏與公主長(zhǎng)得像评姨,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子萤晴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353