IOS 自定义滑动Tab(顶部) 带滑动的条条


// 代码写得也比较粗糙, 希望能有帮助吧

// 看下文件的目录





// 开始代码啦 ********************


//

//  WWCSwitchTabViewController.h

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "WWCSwitchTabView.h"


#import "WWCFavoriteTabView.h"

#import "WWCBestLoveTabView.h"

#import "WWHistoryTabView.h"


#define tabViewHeight 40


@interface WWCSwitchTabViewController :UIViewController <SwitchTabTestDelegate>

{

    


    WWCSwitchTabView *switchTabView;

    

    

    NSArray *data;

    

    

    WWCFavoriteTabView *favoriteView;

    WWCBestLoveTabView *bestLoveView;

    WWHistoryTabView *historyView;

    

    NSArray *arrVies;

}



@end


//

//  WWCSwitchTabViewController.m

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import "WWCSwitchTabViewController.h"

#import "GeneralMarro.h"


@interface WWCSwitchTabViewController ()


@end


@implementation WWCSwitchTabViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    self.title =@"SwitchTest";

    

    [self loadSelfLayoutSubviews];

    

}


-(void)loadSelfLayoutSubviews

{

    

    data = [[NSArray alloc] initWithObjects:@"收藏",@"最爱",@"历史",nil];

    

    switchTabView = [[WWCSwitchTabView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, tabViewHeight)];

    switchTabView.switchTabDelegate = self;

    

    [self.view addSubview:switchTabView];

    

    

    [self initSwitchView];

}



-(void)initSwitchView

{

    CGFloat height = CGRectGetHeight(self.view.frame) - 64 ;

    CGFloat width = CGRectGetWidth(self.view.frame);

    

    

    CGRect allFrame = CGRectMake(0, tabViewHeight, width, height);

    

    favoriteView  =[[WWCFavoriteTabView alloc] initWithFrame:allFrame];

    favoriteView.backgroundColor = [UIColor redColor] ;//Color(46, 48, 59, 1);

    

    bestLoveView = [[WWCBestLoveTabView alloc] initWithFrame:allFrame];

    bestLoveView.backgroundColor =  [UIColor purpleColor];//Color(46, 48, 99, 1);

    

    historyView = [[WWHistoryTabView alloc] initWithFrame:allFrame];

    historyView.backgroundColor = [UIColor magentaColor];//Color(46, 66, 99, 1);

    

    arrVies = [[NSArray alloc] initWithObjects:favoriteView,bestLoveView,historyView,nil];

    

    

    

    [self.view addSubview:favoriteView];

    [self.view addSubview:historyView];

    [self.view addSubview:bestLoveView];

    

    

    // 默认显示收藏/0

    [self showCurrentView:[arrVies objectAtIndex:0]];

    

    

}


// 显示指定的View


-(void)showCurrentView:(UIView *)view

{

    

    for (int i =0; i < arrVies.count; i ++ ) {

        UIView *view  = [arrViesobjectAtIndex:i];

        view.hidden =YES;

    }

    

    view.hidden =NO;

    

    

}


#pragma mark - SwitchTabDelegate 

-(NSInteger)numberOfSwitchTabView

{

    returndata.count;

}


-(NSString *)titleOfSwithcTabViewatIndex:(NSInteger)indexRow

{

    

    return [dataobjectAtIndex:indexRow];

}


-(void)didSelectedSwitchTabView:(WWCSwitchTabView *)switchView atIndex:(NSInteger)indexRow

{

    

    [selfshowCurrentView:[arrViesobjectAtIndex:indexRow]];

}


-(UIColor *)colorForBackground:(NSInteger)indexRow// 返回指定位置的背景颜色

{

    return  [UIColorbrownColor];

}


-(UIColor *)colorForTitle:(NSInteger)indexRow// 返回指定标题字体颜色

{

    return  [UIColorwhiteColor];

}


-(UIColor *)colorForTitle_selected:(NSInteger)indexRow// 返回选中指定标题字体颜色

{

    return [UIColor blueColor];

}


-(UIFont *)fontForSwitchTitle

{

    return [UIFont systemFontOfSize:14];

}


-(UIColor *)footLineColor

{

    return [UIColor blueColor];

}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


// 自定义的TabView ************

//

//  WWCSwitchTabView.h

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import <UIKit/UIKit.h>




@class WWCSwitchTabView;



@protocol SwitchTabTestDelegate <NSObject>


@required //要求


-(NSInteger)numberOfSwitchTabView;// 返回的个数

-(NSString *)titleOfSwithcTabViewatIndex:(NSInteger)indexRow;//返回指定位置的标题

// 点击(滑动)事件

-(void)didSelectedSwitchTabView:(WWCSwitchTabView *)switchView atIndex:(NSInteger)indexRow;


@optional //可选


-(UIColor *)colorForBackground:(NSInteger)indexRow;// 返回指定位置的背景颜色

-(UIColor *)colorForTitle:(NSInteger)indexRow;// 返回指定标题字体颜色

-(UIColor *)colorForTitle_selected:(NSInteger)indexRow;// 返回选中指定标题字体颜色

-(UIFont *)fontForSwitchTitle;

-(UIColor *)footLineColor;


@end


@interface WWCSwitchTabView :UIView

{


    

    UIView *footLineView;

    CGFloat tab_width ;

}


@property (assign)NSInteger switchTabCount;

@property (copy,nonatomic) NSString *switchTabTitle;

@property (assign)CGFloat tab_width;


@property (strong,nonatomic) UIColor *bgColor;

@property (strong,nonatomic) UIColor *titleColor;

@property (assign)UIColor *titleSelectedColor;

@property (assign)UIColor *footLineColor;




@property (strong,nonatomic) id<SwitchTabTestDelegate> switchTabDelegate;




@end




//

//  WWCSwitchTabView.m

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import "WWCSwitchTabView.h"


@implementation WWCSwitchTabView



-(id)initWithFrame:(CGRect)frame

{

    

    self = [superinitWithFrame:frame];

    if (self) {

        [selfloadSelfLayoutViews];

    }

    returnself;

}


-(void)loadSelfLayoutViews

{

    

    NSLog(@".... loadSelfLayoutViews ...");

}



-(void)layoutSubviews

{

    

    CGFloat width =self.frame.size.width;

    CGFloat height =CGRectGetHeight(self.frame);

    NSLog(@"width %f" , width);

    

    self.backgroundColor = [UIColorgrayColor];

    

    

    if (self.switchTabDelegate) {

        

       self.switchTabCount = [self.switchTabDelegatenumberOfSwitchTabView];

        

        tab_width = width/self.switchTabCount;

        

        // 初始化切换的tab按钮

        CGFloat y =0;

        for (int i=0; i <self.switchTabCount; i++) {


            CGFloat x = i*tab_width;

            

            UIButton *switchButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

            switchButton.frame =CGRectMake(x, y, tab_width, height);

            switchButton.tag = i +100 ;

            self.switchTabTitle = [self.switchTabDelegatetitleOfSwithcTabViewatIndex:i];

            // 标题

            [switchButton setTitle:self.switchTabTitleforState:UIControlStateNormal];

            // 背景

            [switchButton setBackgroundColor:[self.switchTabDelegatecolorForBackground:i]];

            // 字体颜色

            [switchButton setTitleColor:[self.switchTabDelegatecolorForTitle:i] forState:UIControlStateNormal];

            // 选择字体颜色

            [switchButton setTitleColor:[self.switchTabDelegatecolorForTitle_selected:i] forState:UIControlStateHighlighted];

            // 按钮点击事件

            [switchButton addTarget:selfaction:@selector(switcAction:)forControlEvents:UIControlEventTouchUpInside];

            // 设置字体

            [switchButton.titleLabelsetFont:[self.switchTabDelegatefontForSwitchTitle]];

            

            [selfaddSubview:switchButton];

            

        }

        

        // 初始化底部Line

        

        CGFloat l_x =5;

        CGFloat l_y = height -2;

        CGFloat l_w =tab_width- 10;

        CGFloat l_h =2;

        

        footLineView = [[UIViewalloc] initWithFrame:CGRectMake(l_x, l_y, l_w, l_h)];

        footLineView.backgroundColor = [self.switchTabDelegatefootLineColor];

        [selfaddSubview:footLineView];

    }

    

}


-(void)switcAction:(UIButton *)sender

{

    UIButton *button = (UIButton *)sender;

    

    if (self.switchTabDelegate ) {

        

        [UIViewanimateWithDuration:0.5animations:^{

            

            CGRect frame =footLineView.frame;

            frame.origin.x =5 +((button.tag-100) *tab_width);

            footLineView.frame = frame;

            

        }completion:^(BOOL isFinished){

            

        }];

        

        [self.switchTabDelegatedidSelectedSwitchTabView:selfatIndex:button.tag-100];

    }

    

}


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/




@end


// ************************ 切换的3个视图


//

//  WWCFavoriteTabView.h

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface WWCFavoriteTabView :UIView


@end



//

//  WWCFavoriteTabView.m

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/8/7.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import "WWCFavoriteTabView.h"


@implementation WWCFavoriteTabView


/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end




// 另外2个跟FavoriteView 一样的, 里面可以自定义自己所需要的内容



代码结束




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值