先上要实现的效果:
有人问,问什么动画效果这么慢?答案是下图
我拿到这个需求的第一反应就是,需要自己写个TabBar了,因为系统的TabBar根本没有这种效果。既然这样,那我们肯定不能继承UITabBar去写了,只能是自己写一个控件
1:先把大的布局写出来,一个TabBarVc中包含5个嵌套了NavigationController的控制器,代码就不上了
2:移除系统自带的TabBar
pragma mark - 移除掉系统的TabBar上的子控件
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// 把系统的tabBar上的按钮干掉
for (UIView *childView in self.tabBar.subviews) {
[childView removeFromSuperview];
}
}
// 移除系统的TabBar
[self.tabBar removeFromSuperview]
- 3:换上我自己写的TabBar,这里的
self.btnItems
中收集的是系统的UITabItem
模型
- (void)setupTabBar
{
// 移除系统的TabBar,移除后,并不会马上消失,一般在下一次循环时,判断这个对象有没有强引用,没有的话,才销毁
[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内部实现
.h中
#import <UIKit/UIKit.h>
@class LYTabBar;
@protocol LYTabBarDelegate <NSObject>
- (void)tabBar:(LYTabBar *)tabBar didClickIndex:(NSUInteger)index;
@end
@interface LYTabBar : UIView
// 模型数组(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;
}
// 先默认设置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
// 先默认设置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