1.导入头文件
2.创建类SliderContentViewController继承于UIViewController
//
#import "ViewController.h"
#import "YJSliderView.h"
#import "SliderContentViewController.h"
@interface ViewController ()<YJSliderViewDelegate>
@property (nonatomic, strong) YJSliderView *sliderView;
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, strong) NSArray *titleArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//需要将UIView的自动调整ScrollViewInset关闭
self.automaticallyAdjustsScrollViewInsets = NO;
self.sliderView = [[YJSliderView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
self.sliderView.delegate = self;
self.titleArray = @[@"灰色", @"黄色黄色", @"紫", @"橘色", @"蓝色"];
[self.view addSubview:self.sliderView];
}
//这里是设置显示的title个数
- (NSInteger)numberOfItemsInYJSliderView:(YJSliderView *)sliderView {
return _titleArray.count;
}
- (UIView *)yj_SliderView:(YJSliderView *)sliderView viewForItemAtIndex:(NSInteger)index {
SliderContentViewController *vc = [[SliderContentViewController alloc] init];
if (index == 0) {
vc.view.backgroundColor = [UIColor lightGrayColor];
} else if (index == 1) {
vc.view.backgroundColor = [UIColor yellowColor];
} else if (index == 2) {
vc.view.backgroundColor = [UIColor purpleColor];
} else {
vc.view.backgroundColor = [UIColor orangeColor];
}
return vc.view;
}
//设置显示的title
- (NSString *)yj_SliderView:(YJSliderView *)sliderView titleForItemAtIndex:(NSInteger)index {
return self.titleArray[index];
}
//设置默认选择的title
- (NSInteger)initialzeIndexFoYJSliderView:(YJSliderView *)sliderView {
return 3;
}