ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar

我又来啦····最近在准备实习,渣渣的工作路程就要开始啦啦啦啦····

闲话少说,进入正题吧~····之前在做项目的时候很多时候用到tabBarViewController这个东西,ios uikit自带的UITabBarViewController虽然可以直接用,但是我发现有几个局限性:

1、不带滑动手势。现在很多app的tabBar都是带有滑动的手势,滑动翻页这种交互不仅让人感觉很酷,而且也方便操作,不用去点击下面的tabBar。但是UIKit里面竟然没有,挺失望的

2、没有滑动翻页的效果。滑动翻页手势没有嘛就算了···但是竟然连滑动翻页的效果也没有·····这样就感觉比较死板,如果想做一个娱乐性比较强的app的话就不行啦

3、不可以自定义tabBar,原声的tabBar样式比较难改。这是最头疼的了···因为设计师往往都会要求把这个地方给改了···原声的确实不太好看,而且没有个性,虽然网上也有一些方法可以实现自定义tabBar的效果,比如隐藏掉原来的tabBar··自己写一个啦什么的··我之前也是这样干的,勉强能够满足需求吧,但这明显不是最好的办法


基于以上几个理由···我就小牛试刀自己写了个带滑动手势,了可以通过设置参数选择是否需要翻页手势和滑动效果,并且可以用自己的UIButton作为tabBar的tabBarViewController了,原理其实很简单,要达到滑动手势的效果,就直接用UIScrollView以及它的几个委托函数就可以了,tabBar就直接用UIButton,提供给用户设置button数组的接口,内部实现点击事件就ok了。。。具体看代码吧,注释应该算是比较清楚的了,有问题请留言~···thx:

同样,头文件

//
//  KKTabBarViewController.h
//  KKTabBarViewDemo
//
//  Created by yaodd on 14-1-26.
//  Copyright (c) 2014年 yaodd. All rights reserved.
//
//  带滑动手势的tabBarViewController,可自定义tabBar,只需要传入自己自定义的UIButton数组即可,

#import "ViewController.h"
typedef NS_ENUM(NSInteger, TabBarPosition){
    kTabBarPositionTop,
    kTabBarPositionBottom
};

@interface KKTabBarViewController : ViewController 
@property (nonatomic, assign) int curPage;                              //当前选中的页面
@property (nonatomic, assign) BOOL tabBarHidden;                        //是否隐藏底部的tabBar
@property (nonatomic, assign) BOOL transitionAnimated;                  //点击tabBar是否有滑动翻页效果
@property (nonatomic, assign) BOOL suppurtGestureTransition;            //是否支持手势滑动
@property (nonatomic, assign) CGFloat tabBarHeight;                     //tabBar的高度,意味着可在非初始化时修改tabBar的高度
@property (nonatomic, strong) NSArray *controllerArray;                 //controller的数组
@property (nonatomic, strong) NSArray *tabButtonArray;                  //底部tabBar的数组,为自定义button
@property (nonatomic, assign) TabBarPosition tabBarPosition;            //tabBar的位置,可选底部和顶部,默认底部


//带参数的初始化函数
- (id)initWithControllerArray:(NSArray *)controllerArray;
//设置是否隐藏掉切换页面的按钮
- (void)setTabBarHidden:(BOOL)tabBarHidden;
@end
实现文件:

//
//  KKTabBarViewController.m
//  KKTabBarViewDemo
//
//  Created by yaodd on 14-1-26.
//  Copyright (c) 2014年 yaodd. All rights reserved.
//

#import "KKTabBarViewController.h"

@interface KKTabBarViewController ()

@property (nonatomic, strong) UIScrollView *tabScrollView;
@property (nonatomic, assign) CGFloat pageWidth;
@property (nonatomic, assign) CGFloat pageHeight;


@end

@implementation KKTabBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}
- (id)initWithControllerArray:(NSArray *)controllerArray
{
    _controllerArray = [NSArray arrayWithArray:controllerArray];
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self initTabPage];
    [self initTabBar];
    
    
}

- (id)init{
    self = [super init];
    if (self) {
        _tabBarPosition = kTabBarPositionBottom;
        _tabBarHidden = NO;
        _transitionAnimated = YES;
        _suppurtGestureTransition = YES;
    }
    return  self;
}

//初始化每个页面,把分页加进去
- (void)initTabPage
{
    _pageWidth = self.view.frame.size.width;
    _pageHeight = self.view.frame.size.height;
    CGRect scrollFrame = CGRectMake(0, 0, _pageWidth, _pageHeight);
    CGSize contentSize = CGSizeMake(_pageWidth * [_controllerArray count], _pageHeight);
    _tabScrollView = [[UIScrollView alloc]initWithFrame:scrollFrame];
    [_tabScrollView setContentSize:contentSize];
    [_tabScrollView setPagingEnabled:YES];
    [_tabScrollView setDelegate:self];
    [_tabScrollView setShowsHorizontalScrollIndicator:NO];
    [_tabScrollView setShowsVerticalScrollIndicator:NO];
    [_tabScrollView setScrollEnabled:_suppurtGestureTransition];
    //把分页的view加进scrollview
    for (int i = 0; i < [_controllerArray count]; i ++) {
        //从controller数组中取出controller的view
        UIView *tabView = ((UIViewController *)[_controllerArray objectAtIndex:i]).view;
        [tabView setFrame:CGRectMake(i * _pageWidth, 0, tabView.frame.size.width, tabView.frame.size.height)];
        [_tabScrollView addSubview:tabView];
        
    }
    [self.view addSubview:_tabScrollView];
    
    _curPage = 0;
    _tabBarHidden = NO;
}
//初始化切换页面的按钮
- (void)initTabBar
{
    CGFloat buttonWidth = _pageWidth / [_controllerArray count];
    for (int i = 0 ; i < [_tabButtonArray count]; i ++) {
        CGFloat buttonHeight;
        CGFloat buttonY;
        
        UIButton *tabButton = [_tabButtonArray objectAtIndex:i];
        if (tabButton.frame.size.height == 0) {
            buttonHeight = 50;
        } else{
            buttonHeight = tabButton.frame.size.height;
        }
        if (_tabBarPosition == kTabBarPositionTop) {
            buttonY = 0;
        } else{
            buttonY = _pageHeight - buttonHeight;
        }
        [tabButton setFrame:CGRectMake(buttonWidth * i, buttonY, buttonWidth, buttonHeight)];
        NSLog(@"%f height",tabButton.frame.size.height);
        [tabButton setTag:i];
        [tabButton addTarget:self action:@selector(tabAction:) forControlEvents:UIControlEventTouchDown];
        [tabButton setHidden:self.tabBarHidden];
        if (tabButton.tag == _curPage) {
            [tabButton setSelected:YES];
        }
        [self.view addSubview:tabButton];
    }
    
}
//设置是否隐藏掉切换页面的按钮
- (void)setTabBarHidden:(BOOL)tabBarHidden
{
    self.tabBarHeight = tabBarHidden;
    NSLog(@"%d subView",[[self.view subviews] count]);
    for (UIButton *tabButton  in _tabButtonArray) {
        [tabButton setHidden:self.tabBarHeight];
//        [tabButton setHidden:YES];
    }
}
//设置底部按钮的高度
- (void)setTabBarHeight:(CGFloat)tabBarHeight
{
    _tabBarHeight = tabBarHeight;
    NSLog(@"height %f",self.view.frame.size.height);
    for (UIButton *button in _tabButtonArray) {
        CGRect buttonFrame = button.frame;
        buttonFrame.size.height = _tabBarHeight;
        if (_tabBarPosition == kTabBarPositionTop) {
            buttonFrame.origin.y = 0;
        } else{
            buttonFrame.origin.y = self.view.frame.size.height - _tabBarHeight;
        }
        button.frame = buttonFrame;
    }
}
- (void)setTabBarPosition:(TabBarPosition)tabBarPosition
{
    _tabBarPosition = tabBarPosition;
    for (UIButton *button in _tabButtonArray) {
        CGRect buttonFrame = button.frame;
        if (_tabBarPosition == kTabBarPositionTop) {
            buttonFrame.origin.y = 0;
        } else{
            buttonFrame.origin.y = _pageHeight - buttonFrame.size.height;
        }
        button.frame = buttonFrame;
        
    }
}
- (void)setSuppurtGestureTransition:(BOOL)suppurtGestureTransition
{
    _suppurtGestureTransition = suppurtGestureTransition;
    [_tabScrollView setScrollEnabled:_suppurtGestureTransition];
}
//切换页面按钮点击事件
- (void)tabAction:(UIButton *)button
{
    [_tabScrollView setContentOffset:CGPointMake(button.tag * _pageWidth, 0) animated:_transitionAnimated];
    for (UIButton *tempButton in _tabButtonArray) {
        if (button.tag != tempButton.tag) {
            [tempButton setSelected:NO];
        }
    }
    [button setSelected:YES];
    
}
#pragma UIScrollViewDelegate mark
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    CGFloat offset = [scrollView contentOffset].x;
    int curPage = offset / _pageWidth;
    for (UIButton *button in _tabButtonArray) {
        if (button.tag == curPage) {
            [button setSelected:YES];
        }
        else{
            [button setSelected:NO];
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = [scrollView contentOffset].x;
    _curPage = offset / _pageWidth;
    for (UIButton *button in _tabButtonArray) {
        if (button.tag == _curPage) {
            [button setSelected:YES];
        }
        else{
            [button setSelected:NO];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


需要完整工程代码的移步http://download.csdn.net/detail/kekeqiaokeli/6993011  多谢合作!

好好学习···天天向上~···啦啦啦


你可能感兴趣的:(iOS)