导入 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;
}
}