//
// ViewController.m
// wyy
//
// Copyright © 2016年 zm. All rights reserved.
//
#import "ViewController.h"
#import "ZMViewController.h"
#import "ZMLabel.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrllView;
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.title = @"网易新闻";
[self setUpChildVC];
[self setUpTitle];
//默认选中第一页 调用此方法
[self scrollViewDidEndScrollingAnimation:self.contentScrollview];
}
//创建头部标题视图
- (void)setUpTitle{
CGFloat w = 100;
CGFloat y = 0;
CGFloat h = self.titleScrllView.frame.size.height;
for (int i = 0; i < 7; i++) {
ZMLabel *lable = [[ZMLabel alloc]initWithFrame:CGRectMake(i*w, y, w, h)];
lable.text = [self.childViewControllers[i] title];
//给lable添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[lable addGestureRecognizer:tap];
[self.titleScrllView addSubview:lable];
}
self.titleScrllView.contentSize = CGSizeMake(7*w, 0);
self.contentScrollview.contentSize = CGSizeMake(7*[UIScreen mainScreen].bounds.size.width, 0);
}
- (void)tap:(UITapGestureRecognizer *)tap{
NSInteger index = [self.titleScrllView.subviews indexOfObject:tap.view];
//通过手势设置内容滚动视图的偏移量
[self.contentScrollview setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width * index, 0) animated:YES];
}
//当动画结束时调用此方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
CGFloat w = scrollView.frame.size.width;
CGFloat h = scrollView.frame.size.height;
CGFloat offsetX = scrollView.contentOffset.x;
NSInteger index = offsetX/w;
ZMLabel *lab = self.titleScrllView.subviews[index];
CGFloat offsetx = lab.center.x - scrollView.frame.size.width*0.5;
//判断offsetx最大最小值两种情况
if (offsetx < 0) {
offsetx = 0;
}
if (offsetx > (self.titleScrllView.contentSize.width - scrollView.frame.size.width)) {
offsetx = self.titleScrllView.contentSize.width - scrollView.frame.size.width;
}
self.titleScrllView.contentOffset = CGPointMake(offsetx, 0);
UIViewController *zm = self.childViewControllers[index];
if ([zm isViewLoaded]) return;
zm.view.frame = CGRectMake(index *w, 0, w, h);
[self.contentScrollview addSubview:zm.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self scrollViewDidEndScrollingAnimation:scrollView];
}
- (void)setUpChildVC{
ZMViewController *zm = [[ZMViewController alloc]init];
zm.title = @"新闻";
[self addChildViewController:zm];
ZMViewController *zm1 = [[ZMViewController alloc]init];
zm1.title = @"军事";
[self addChildViewController:zm1];
ZMViewController *zm2 = [[ZMViewController alloc]init];
zm2.title = @"政治";
[self addChildViewController:zm2];
ZMViewController *zm3 = [[ZMViewController alloc]init];
zm3.title = @"娱乐";
[self addChildViewController:zm3];
ZMViewController *zm4 = [[ZMViewController alloc]init];
zm4.title = @"社会";
[self addChildViewController:zm4];
ZMViewController *zm5 = [[ZMViewController alloc]init];
zm5.title = @"科技";
[self addChildViewController:zm5];
ZMViewController *zm6 = [[ZMViewController alloc]init];
zm6.title = @"啦啦";
[self addChildViewController:zm6];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat scale = scrollView.contentOffset.x/scrollView.frame.size.width;
NSInteger leftIndex = scale;
NSInteger rightIndex = scale+1;
if (scale < 0 || scale > self.titleScrllView.subviews.count-1) return;
CGFloat rightScale = scale - leftIndex;
CGFloat leftScale = 1-rightScale;
ZMLabel *leftLabel = self.titleScrllView.subviews[leftIndex];
ZMLabel *rightLabel = (rightIndex == self.titleScrllView.subviews.count)?nil:(self.titleScrllView.subviews[rightIndex]);
leftLabel.scale = leftScale;
rightLabel.scale = rightScale;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// 自定义一个lable 继承于uilable
//
// ZMLabel.m
// wyy
//
// Copyright © 2016年 zm. All rights reserved.
//
#import "ZMLabel.h"
@implementation ZMLabel
- (instancetype)initWithFrame:(CGRect)frame{
if (self =[super initWithFrame:frame]) {
self.textAlignment = NSTextAlignmentCenter;
self.userInteractionEnabled = YES;
self.textColor = [UIColor blackColor];
self.font = [UIFont systemFontOfSize:15];
}
return self;
}
- (void)setScale:(CGFloat)scale{
CGFloat transform = scale*0.3+1;
self.transform = CGAffineTransformMakeScale(transform, transform);
self.textColor = [UIColor colorWithRed:transform-1 green:0 blue:0 alpha:1];
}
@end
//创建控制器 这里可以为了简易就只创建一个控制器
//
// ZMViewController.m
// wyy
//
// Copyright © 2016年 zm. All rights reserved.
//
#import "ZMViewController.h"
@interface ZMViewController ()
@end
@implementation ZMViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@-%ld",self.title,indexPath.row];
return cell;
}
@end