自定義TabBar

先上要實(shí)現(xiàn)的效果:


要實(shí)現(xiàn)的效果.gif

有人問,問什么動畫效果這么慢?答案是下圖


這里設(shè)置慢動畫.png

我拿到這個需求的第一反應(yīng)就是,需要自己寫個TabBar了,因?yàn)橄到y(tǒng)的TabBar根本沒有這種效果。既然這樣,那我們肯定不能繼承UITabBar去寫了,只能是自己寫一個控件

  • 1:先把大的布局寫出來,一個TabBarVc中包含5個嵌套了NavigationController的控制器,代碼就不上了

  • 2:移除系統(tǒng)自帶的TabBar

pragma mark - 移除掉系統(tǒng)的TabBar上的子控件
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 把系統(tǒng)的tabBar上的按鈕干掉
    for (UIView *childView in self.tabBar.subviews) {
        [childView removeFromSuperview];
    }
}

// 移除系統(tǒng)的TabBar
[self.tabBar removeFromSuperview]
  • 3:換上我自己寫的TabBar,這里的self.btnItems中收集的是系統(tǒng)的UITabItem模型
- (void)setupTabBar
{
    // 移除系統(tǒng)的TabBar,移除后,并不會馬上消失,一般在下一次循環(huán)時,判斷這個對象有沒有強(qiáng)引用,沒有的話,才銷毀
    [self.tabBar removeFromSuperview];
    // 自定義的
    LYTabBar *tabBar = [[LYTabBar alloc] init];
    tabBar.frame = CGRectMake(0, kScreenHeight-64, kScreenWidth, 64);
    tabBar.delegate = self;
    tabBar.items = self.btnItems;
    [self.view addSubview:tabBar];
    self.customTabBar = tabBar;
}
  • 4、自己寫的TabBar內(nèi)部實(shí)現(xiàn)

.h中

#import <UIKit/UIKit.h>
@class LYTabBar;
@protocol LYTabBarDelegate <NSObject>

- (void)tabBar:(LYTabBar *)tabBar didClickIndex:(NSUInteger)index;

@end

@interface LYTabBar : UIView

// 模型數(shù)組(UITabBarItem)
@property (nonatomic, strong) NSArray *items;

/** 代理 */
@property (nonatomic, assign) id<LYTabBarDelegate> delegate;

@end

#pragma mark - 自定義控件
@interface LYCustomTabBarView : UIView

/** title */
@property (nonatomic, strong) UILabel *title;

/** norImg */
@property (nonatomic, strong) UIImageView *norImg;

/** select */
@property (nonatomic, strong) UIImageView *selectImg;

- (instancetype)initWithTitle:(NSString *)title imgName:(UIImage *)norImg selectImgName:(UIImage *)selectImg;

@end

.m中

#import "LYTabBar.h"

#define custom_w  kScreenWidth/5
#define custom_h  64

@interface LYTabBar ()

/** lastNorImg */
@property (nonatomic, strong) UIImageView *lastnorImg;
/** lastselectImg */
@property (nonatomic, strong) UIImageView *lastselectImg;

@end

@implementation LYTabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}
// 先默認(rèn)設(shè)置64的高度
- (void)setItems:(NSArray *)items
{
    _items = items;
    for (int i = 0; i<items.count; i++) {
        UITabBarItem *item = items[i];
        
        LYCustomTabBarView *custom = [[LYCustomTabBarView alloc] initWithTitle:item.title imgName:item.image selectImgName:item.selectedImage];
        custom.frame = CGRectMake(custom_w*i, 0, custom_w, custom_h);
        [custom.norImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(norImgTapAction:)]];
        [custom.selectImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImgTapAction:)]];
        [self addSubview:custom];
    }
}

#pragma mark - 動作按鈕事件
- (void)norImgTapAction:(UITapGestureRecognizer *)sender
{
    UIImageView *nor = (UIImageView *)[sender view];
    UIImageView *select = [nor.superview viewWithTag:2001];
    [UIView animateWithDuration:0.25 animations:^{
        if (self.lastnorImg&&self.lastselectImg) {
            self.lastselectImg.alpha = 0;
            self.lastnorImg.alpha = 1;
            self.lastselectImg.frame = CGRectMake((custom_w-25)/2, custom_h-20-25, 25, 23);
            self.lastnorImg.frame = CGRectMake((custom_w-25)/2, custom_h-20-25, 25, 23);
        }
        nor.alpha = 0;
        select.alpha = 1;
        select.frame = CGRectMake((custom_w-37.5)/2, 0, 37.5, 34.5);
        nor.frame = select.frame;
        
        self.lastnorImg = nor;
        self.lastselectImg = select;
    }];
    
    if ([self.delegate respondsToSelector:@selector(tabBar:didClickIndex:)]) {
        NSUInteger count = [self.subviews indexOfObject:nor.superview];
        [self.delegate tabBar:self didClickIndex:count];
    }
    
}

- (void)selectImgTapAction:(UITapGestureRecognizer *)sender
{
    UIImageView *select = (UIImageView *)[sender view];
    UIImageView *nor = [select.superview viewWithTag:2000];
    [UIView animateWithDuration:0.25 animations:^{
        if (self.lastnorImg&&self.lastselectImg) {
            self.lastselectImg.alpha = 0;
            self.lastnorImg.alpha = 1;
            self.lastselectImg.frame = CGRectMake((custom_w-25)/2, custom_h-20-25, 25, 23);
            self.lastnorImg.frame = CGRectMake((custom_w-25)/2, custom_h-20-25, 25, 23);
        }
        nor.alpha = 1;
        select.alpha = 0;
        self.lastnorImg = nor;
        self.lastselectImg = select;
    }];
}

@end

#pragma mark - 自定義控件
@implementation LYCustomTabBarView
// 先默認(rèn)設(shè)置64的高度
- (instancetype)initWithTitle:(NSString *)title imgName:(UIImage *)norImg selectImgName:(UIImage *)selectImg
{
    self = [super init];
    if (self) {
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = title.length?title:@"微信";
        titleLabel.font = [UIFont systemFontOfSize:14.0f];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.frame = CGRectMake(0, custom_h-20, custom_w, 20);
        [self addSubview:titleLabel];
        self.title = titleLabel;
        
        UIImageView *norImgView = [[UIImageView alloc] init];
        norImgView.image = norImg;
        norImgView.userInteractionEnabled = YES;
        norImgView.tag = 2000;
        norImgView.frame = CGRectMake((custom_w-25)/2, self.title.ly_y-25, 25, 23);
        norImgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:norImgView];
        self.norImg = norImgView;
        
        UIImageView *selectImgView = [[UIImageView alloc] init];
        selectImgView.image = selectImg;
        selectImgView.userInteractionEnabled = YES;
        selectImgView.alpha = 0;
        selectImgView.tag = 2001;
        selectImgView.frame = CGRectMake((custom_w-25)/2, self.title.ly_y-25, 25, 23);
        selectImgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:selectImgView];
        self.selectImg = selectImgView;
    }
    return self;
}

@end

demo地址

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

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

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