这里我知道的有两种方法,不知道其他人有没有比较好的方法可以留言给我哈。。。
第一种方法是创建多个ViewController,再用一个ViewController包含前面的创建的ViewController(我觉得这种比较好处理后面每个包含的ViewController里面的事情)
第二种方法是创建多个View,再用一个ViewController包含前面的View
好,先祭上第一种方法的代码
这里需要用到两个文件:一个负责切换的文字,一个负责切换的内容,分别是SegmentTapView,FlipTableView;
<pre name="code" class="objc">//设置大小
-(instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray *)dataArray withFont:(CGFloat)font
//添加内容
-(void)addSubSegmentView
//点击后的操作
-(void)tapAction:(id)sender{
//设置每个页面的index
-(void)selectIndex:(NSInteger)index
</pre>//下面的就是设置一些字体相关的什么的<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"></p><pre name="code" class="objc">-(void)setLineColor:(UIColor *)lineColor{
if (_lineColor != lineColor) {
self.lineImageView.backgroundColor = lineColor;
_lineColor = lineColor;
}
}
//接下来是页面里面的内容,因为是切换,所以定义为tableview
//同样的,先是设置大小
-(instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)contentArray
//接下来就是跟tableview的内容
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//同样的,也要设置每个页面的index
-(void)selectIndex:(NSInteger)index
//最后,需要将你创建的页面加入进来
//初始化segment
-(void)initSegment{
self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0, 64, ScreeFrame.size.width, 40) withDataArray:[NSArray arrayWithObjects:@"first",@"second",@"third",@"fouth", nil] withFont:15];
self.segment.delegate = self;
[self.view addSubview:self.segment];
}
</pre><pre name="code" class="objc">//初始化<span style="font-family: Menlo;">FlipTableView</span>
-(void)initFlipTableView{
if (!self.controllsArray) {
self.controllsArray = [[NSMutableArray alloc] init];
}
ViewController1 *v1 = [[UIStoryboard storyboardWithName:@"Body" bundle:nil] instantiateViewControllerWithIdentifier:@"first"];
ViewController2 *v2 = [[UIStoryboard storyboardWithName:@"Body" bundle:nil] instantiateViewControllerWithIdentifier:@"second"];
ViewController3 *v3 = [[UIStoryboard storyboardWithName:@"Body" bundle:nil] instantiateViewControllerWithIdentifier:@"third"];
ViewController4 *v4 = [[UIStoryboard storyboardWithName:@"Body" bundle:nil] instantiateViewControllerWithIdentifier:@"fouth"];
[self.controllsArray addObject:v1];
[self.controllsArray addObject:v2];
[self.controllsArray addObject:v3];
[self.controllsArray addObject:v4];
self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0, 104, ScreeFrame.size.width, self.view.frame.size.height - 104) withArray:_controllsArray];
self.flipView.delegate = self;
[self.view addSubview:self.flipView];
}
#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
NSLog(@"%ld",index);
[self.flipView selectIndex:index];
}
-(void)scrollChangeToIndex:(NSInteger)index
{
NSLog(@"%ld",index);
[self.segment selectIndex:index];
}
-------分割线-------
下面介绍第二种方法
类似上面,也是一个负责切换的文字,一个负责切换的内容,分别是XTSegmentControl,iCarousel;
先是XTSegmentControl的代码
- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)titleItem delegate:(id <XTSegmentControlDelegate>)delegate;
- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)titleItem selectedBlock:(XTSegmentControlBlock)selectedHandle;
- (void)selectIndex:(NSInteger)index;
- (void)moveIndexWithProgress:(float)progress;
- (void)endMoveIndex:(NSInteger)index;
_carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 98, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-98)];
_carousel.backgroundColor = [UIColor whiteColor];
_carousel.dataSource = self;
_carousel.delegate = self;
_carousel.decelerationRate = 0.7;
_carousel.type = iCarouselTypeLinear;
_carousel.pagingEnabled = YES;
_carousel.edgeRecognition = YES;
_carousel.bounceDistance = 0.4;
[self.view addSubview:_carousel];
NSArray * title = @[@"1",@"2"];
__weak typeof (_carousel) weakCarousel = _carousel;
_segmentControl = [[XTSegmentControl alloc] initWithFrame:CGRectMake(0, kIOS7DIS(64), CGRectGetWidth(self.view.bounds), 36) Items:title selectedBlock:^(NSInteger index) {
[weakCarousel scrollToItemAtIndex:index animated:NO];
}];
//添加view
[self.view addSubview:_segmentControl];