先看看效果啊. 吼吼. 其实效果都不是我的成果, 我讲的重点在集成!
resetButton 是对每个btn设置图片,
接下来是两个委托函数.
只巾一部分啊. 见注释的部分, 注释掉就行了!!
底下细节处理的都很到位. 居然不支持动画, 大家看完下个程序看看吧.或者直接用一下.
http://ubluesky.com/thankU.html
废话少说, 开始!
主要用到的是
DMFilterView
github: https://github.com/Dimillian/DMFilterView
只是他只是个View视图, 在使用StoryBoard 时, 不是很方便.
开始集成.
不要告诉我, 你项目里没有添加DMFilterView.
//
// MyTabBarViewController.h
#import <UIKit/UIKit.h>
#import "DMFilterView.h"
@interface MyTabBarViewController : UITabBarController
<DMFilterViewDelegate>
@property (nonatomic, strong) DMFilterView *filterView;
+(MyTabBarViewController *)defaultTabBar;
@end
@implementation MyTabBarViewController
static MyTabBarViewController * sharedTabBar;
+(MyTabBarViewController *)defaultTabBar
{
return sharedTabBar; //便于操作, 这样可以很好的控制显示和隐藏.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
/*
for (UIView *v in self.tabBar.subviews) {
[v removeFromSuperview];
}*/
[self.tabBar setHidden:YES];//重点是把他隐藏了,因为你看见的并不是个tabBar.
//基本的初始化方法, 可以参照 Demo
NSArray *array = [[NSArray alloc]initWithObjects:@"",@"",@"",@"", nil]; //全是空的. 目的是后面在按钮上加图片,这样可控性就更大了.也更美观
_filterView = [[DMFilterView alloc]initWithStrings:array containerView:self.view];
[self.filterView attachToContainerView];
[self.filterView setDelegate:self];
UIColor *color = [UIColor colorWithRed:233/255.f green:46/255.f blue:65/255.f alpha:1.0];
[self.filterView setSelectedItemTopBackgroundColor:color];
[self.filterView setSelectedItemTopBackroundColorHeight:2];
color = [UIColor colorWithRed:251/255.f green:251/255.f blue:251/255.f alpha:1.0];
[self.filterView setSelectedItemBackgroundColor:color];
[self.filterView setBackgroundColor:color];
[self resetButton];
sharedTabBar = self;
}
resetButton 是对每个btn设置图片,
-(void)resetButton
{
NSArray *imgNormalNames = @[@"icon_home_normal",@"icon_search_normal",@"icon_list_normal",
@"icon_car_normal",@"icon_person_normal"];
for (int i = 0; i < self.filterView.buttonArray.count; i++) {
UIButton *btn = [self.filterView.buttonArray objectAtIndex:i];
[btn setImage:[UIImage imageNamed:[imgNormalNames objectAtIndex:i]] forState:UIControlStateNormal];
}
}
接下来是两个委托函数.
#pragma mark - FilterVieW delegate
- (void)filterView:(DMFilterView *)filterView didSelectedAtIndex:(NSInteger)index
{
//此处是实现 UITabarController 自带的一个效果
//点击一次切换到这个界面, 再点击就popToRoot. //再次点击需要修改下 DMFilterView, 见后面, 不需要的话, 可以不改.
if (self.selectedIndex == index) {
UINavigationController *nav = [self.viewControllers objectAtIndex:self.selectedIndex];
[nav popToRootViewControllerAnimated:YES];
}
self.selectedIndex = index;
NSLog(@"%d", index);
//实现切换视图后图片的显示效果
[self resetButton];
NSArray *imgNormalNames = @[@"icon_home_focus",@"icon_search_focus",@"icon_list_focus",
@"icon_car_focus",@"icon_person_focus"];
UIButton *btn = [self.filterView.buttonArray objectAtIndex:index];
[btn setImage:[UIImage imageNamed:[imgNormalNames objectAtIndex:index]] forState:UIControlStateNormal];
}
- (CGFloat )filterViewSelectionAnimationSpeed:(DMFilterView *)filterView
{
//return the default value as example, you don't have to implement this delegate
//if you don't want to modify the selection speed
//Or you can return 0.0 to disable the animation totally
return kAnimationSpeed;
}
//DMFilterView.m 文件找到
- (void)onButton:(id)sender 将其中一部分注释掉就行了. 见代码.
#pragma mark - Action
- (void)onButton:(id)sender
{
UIButton *button = (UIButton *)sender;
/*
for (UIButton *btn in self.subviews) {
if ([btn isKindOfClass:[UIButton class]]) {
if ([button isEqual:btn]) {
[btn setUserInteractionEnabled:NO];
}
else{
[btn setUserInteractionEnabled:YES];
}
}
}*/
CGFloat animationSpeed;
if ([self.delegate respondsToSelector:@selector(filterViewSelectionAnimationSpeed:)]) {
animationSpeed = [self.delegate filterViewSelectionAnimationSpeed:self];
}
else{
animationSpeed = kAnimationSpeed;
}
只巾一部分啊. 见注释的部分, 注释掉就行了!!
将StoryBoard 里的TabbarController 的类, 设置成自定义的吧. 看看效果. 吼吼!!
使用起来跟原装的是一样的..
大家有问题可以找我交流
http://ubluesky.com/thankU.html
博客里原创内容不多, 我正在努力, 希望大家见证我的进步.
这篇文章的应用下载, 目前还没有发布到app store. 要用的话可能要越狱了. 下载地址, 点我就行了!