1. 程式人生 > >滾動檢視的導航條 , 加號點選《思路版》

滾動檢視的導航條 , 加號點選《思路版》

效果圖 匯入 XLSlideSwitch 檔案 推薦 , 熱門 , 搞笑 等介面需要建立相對應的ViewController 如圖 我有6個介面 所以就需要建立六個繼承於ViewController的類 接下來 匯入我們的第三方檔案 ( XLSlideSwitch檔案 ) 在ViewController.m介面匯入標頭檔案

#import "ViewController.h"
#import "XLSlideSwitch.h"
***!!!!!以下是六個類的名字 , 這只是我起的名字  你需要更改一下!!!!!***
#import "YiDongTongXunViewController.h"
#import "ChuanMeiViewController.h"
#import "RuanGongViewController.h"
#import "WangGongViewController.h"
#import "YunJiSuanViewController.h"
#import "JianZhuViewController.h"

在 **@interface ViewController ()**後面寫協議和定義一些需要的東西

@interface ViewController ()<XLSlideSwitchDelegate>
{
    UIView *SomeView;   ///是點選加號按鈕出現的View
    UIButton *Btn;   ///按鈕
}
@property (nonatomic , strong)XLSlideSwitch *ScrollView;///滾動檢視

@end

接下來是ViewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    ///建立一個數組用來儲存名字
    NSArray *TitlesArr = @[@"推薦" , @"熱門" ,@"搞笑" , @"軍事" ,  @"社會" , @"音樂"];
    !!!!!用來儲存六個類的名字   需要更改!!!!!!
        NSArray *ControllersArr = @[@"YiDongTongXunViewController" , @"ChuanMeiViewController" , @"RuanGongViewController" , @"WangGongViewController" , @"YunJiSuanViewController" , @"JianZhuViewController"];
    NSMutableArray *ViewControllers = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < TitlesArr.count; i ++) {
        //字串建立控制器
        UIViewController *VC = [[NSClassFromString(ControllersArr[i])alloc] init];
        [ViewControllers addObject:VC];
    }
    //滾動檢視
     _ScrollView = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) Titles:TitlesArr viewControllers:ViewControllers];
    _ScrollView.delegate = self;
    _ScrollView.itemNormalColor = [UIColor darkGrayColor];
    _ScrollView.itemSelectedColor = self.navigationController.navigationBar.tintColor;
    _ScrollView.customTitleSpacing = 30;
    _ScrollView.moreButton = [self moreButton];
    [_ScrollView showInViewController:self];
//    [_ScrollView showInNavigationController:self];
    //View
    SomeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 150, 104, 140, 200)];
    SomeView.backgroundColor = [UIColor orangeColor];
    
    for (int i = 0 ; i < 1; i ++) {
        [self.view addSubview:SomeView];
        SomeView.hidden = YES;
    }
}

新增按鈕的方法

- (UIButton *)moreButton{
    Btn = [[UIButton alloc] init];
//    [button setImage:[UIImage imageNamed:@"channelAdd"] forState:UIControlStateNormal];
    [Btn setTitle:@"➕" forState:UIControlStateNormal];
    [Btn setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    [Btn addTarget:self action:@selector(BtnTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    return Btn;
}

按鈕的點選方法

- (void)BtnTouchUpInside{
//    NSLog(@"點選了新增按鈕");
    if (Btn.selected == YES) {
//        SomeView.hidden = YES;
        Btn.selected = NO;
        SomeView.hidden = YES;
    }else{
        Btn.selected = YES;
        SomeView.hidden = NO;
    }
}