您的位置:首页

网易新闻首页骨架(父子控制器实现)

2016-08-03 00:22 363 查看
一:父子控制器的应用:效果如图:



二:代码

#import "RHMainViewController.h"
#import "RHNewsTableViewController.h"
#import "RHLable.h"
static const CGFloat lableCount = 8;
static const CGFloat lableWidth = 80;
static const CGFloat lableHeight = 40;
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREENWIDTH  [UIScreen mainScreen].bounds.size.width
@interface RHMainViewController ()<UIScrollViewDelegate>
/*顶部标题scrollView*/
@property (nonatomic,weak)UIScrollView *titleScrollView;
/*底部的bottomScrollView*/
@property (nonatomic,weak)UIScrollView *bottomScrollView;
@end

@implementation RHMainViewController

- (void)viewDidLoad {
[super viewDidLoad];

//0:添加子控制器
[self setupChildViewControllers];

//1:创建顶部标题的scrollView
[self setupTopScrollView];

//2:创建底部的scrollView
[self setupBottomScrollView];

//3:默认第一个新闻被选中
[self scrollViewDidEndScrollingAnimation:self.bottomScrollView];
}
- (void)setupTopScrollView {

//1:设置自身属性
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.title = @"网易";
self.automaticallyAdjustsScrollViewInsets = NO;

//2:创建顶部的标题滚动视图
UIScrollView *titleScroll = [[UIScrollView alloc]init];
titleScroll.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
titleScroll.showsVerticalScrollIndicator = NO;
titleScroll.showsHorizontalScrollIndicator = NO;
titleScroll.bounces = NO;
self.titleScrollView = titleScroll;
[self.view addSubview:titleScroll];

//3:创建顶部scrollView上的按钮
for (int i = 0; i <lableCount; i++) {

RHLable *lable = [[RHLable alloc]init];
lable.frame = CGRectMake(i *lableWidth, 0, lableWidth, lableHeight);
[lable addGestureRecognizer:  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]];
lable.text = [self.childViewControllers[i] title];
lable.tag = i;
[titleScroll addSubview:lable];
if (i == 0) {
lable.sca = 1;
}
}

titleScroll.contentSize = CGSizeMake(lableCount *lableWidth, 0);

}
- (void)setupBottomScrollView {

//1:创建底部的scrollView
UIScrollView *bottomScrollView = [[UIScrollView alloc]init];
bottomScrollView.frame = CGRectMake(0,CGRectGetMaxY(self.titleScrollView.frame) ,SCREENWIDTH , SCREENHEIGHT - 64 - 40);
bottomScrollView.bounces = NO;
bottomScrollView.showsVerticalScrollIndicator = NO;
bottomScrollView.showsHorizontalScrollIndicator = NO;
bottomScrollView.pagingEnabled = YES;
bottomScrollView.contentSize = CGSizeMake(lableCount *SCREENWIDTH, 0);
self.bottomScrollView = bottomScrollView;
bottomScrollView.delegate = self;
[self.view addSubview:bottomScrollView];

}

- (void)setupChildViewControllers {

//新闻
RHNewsTableViewController *news = [[RHNewsTableViewController alloc]init];
news.title = @"新闻";
[self addChildViewController:news];

//历史
RHNewsTableViewController *history = [[RHNewsTableViewController alloc]init];
history.title = @"历史";
[self addChildViewController:history];

//阅读
RHNewsTableViewController *read = [[RHNewsTableViewController alloc]init];
read.title = @"阅读";
[self addChildViewController:read];

//视频
RHNewsTableViewController *video = [[RHNewsTableViewController alloc]init];
video.title = @"视频";
[self addChildViewController:video];

//图片
RHNewsTableViewController *image = [[RHNewsTableViewController alloc]init];
image.title = @"图片";
[self addChildViewController:image];

//朋友
RHNewsTableViewController *friend = [[RHNewsTableViewController alloc]init];
friend.title = @"朋友";
[self addChildViewController:friend];

//问答
RHNewsTableViewController *answer = [[RHNewsTableViewController alloc]init];
answer.title = @"问答";
[self addChildViewController:answer];

//知识
RHNewsTableViewController *know = [[RHNewsTableViewController alloc]init];
know.title = @"知识";
[self addChildViewController:know];

}
- (void)tapAction:(UITapGestureRecognizer*)tap {

//1:切换底部的scrollView的视图
NSUInteger index = tap.view.tag;
[self.bottomScrollView setContentOffset:CGPointMake(index *SCREENWIDTH, 0) animated:YES];

}
/**
* scrollView结束了滚动动画以后就会调用这个方法(比如- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;方法执行的动画完毕后)
*/
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

NSInteger index = scrollView.contentOffset.x / SCREENWIDTH;
RHLable *currentLable = self.titleScrollView.subviews[index];

//1:当点击lable时,先将点击的lable自身居中
CGFloat moveX = currentLable.center.x - SCREENWIDTH *0.5;
CGFloat offset = self.titleScrollView.contentSize.width - SCREENWIDTH;
if (moveX < 0)   moveX = 0;
if (moveX > offset)  moveX = offset;

[self.titleScrollView setContentOffset:CGPointMake(moveX, 0) animated:YES];

//2:设置其他lable为正常比例
for (RHLable *lable in self.titleScrollView.subviews) {
if (lable != currentLable) lable.sca = 0.0;

}

UIViewController *new = self.childViewControllers[index];
//如果控制器的view已经加载过了,直接返回
if ([new isViewLoaded]) return;
new.view.frame = CGRectMake(index *SCREENWIDTH, 0, SCREENWIDTH, self.bottomScrollView.frame.size.height);
[self.bottomScrollView addSubview:new.view];

}
/**
* 只要scrollView在滚动,就会调用
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat scale = scrollView.contentOffset.x / SCREENWIDTH;
if (scale < 0 || scale > self.childViewControllers.count -1) return;
NSInteger leftIndex = scale;//将浮点型数转换为整数,小数无论多少全部去掉
NSInteger rightIndex = leftIndex + 1;

//左侧的lable
RHLable *leftLable = self.titleScrollView.subviews[leftIndex];

//右侧的lable
RHLable *rightLable = (rightIndex == self.titleScrollView.subviews.count) ? nil :  self.titleScrollView.subviews[rightIndex];

CGFloat rightScale = scale - leftIndex;
CGFloat leftScale = 1 - rightScale;

leftLable.sca = leftScale;
rightLable.sca = rightScale;

}
/**
* 手指松开scrollView后,scrollView停止减速完毕就会调用这个
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

[self scrollViewDidEndScrollingAnimation:scrollView];
}

@end


#import "RHNewsTableViewController.h"
static NSString *const cellID = @"ID";
@interface RHNewsTableViewController ()

@end

@implementation RHNewsTableViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%ld--------%@",(long)indexPath.row,self.title];
return cell;
}

@end


#import <UIKit/UIKit.h>

@interface RHLable : UILabel
+ (instancetype)lable;
@property (nonatomic,assign)CGFloat sca;
@end


#import "RHLable.h"
//247 86 176
@implementation RHLable

+ (instancetype)lable {

return [[self alloc]init];
}

- (instancetype)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

//1:设置自身的属性
self.font = [UIFont systemFontOfSize:15];
self.textAlignment = NSTextAlignmentCenter;
self.textColor = [UIColor colorWithRed:RHRed green:RHGreen blue:RHBlue alpha:1];
self.userInteractionEnabled = YES;
}

return self;
}

- (void)setSca:(CGFloat)sca {

_sca = sca;

// R G B
// 默认:0.4 0.6 0.7
// 红色:1   0   0

CGFloat redScale = RHRed + (1 - RHRed) *sca;
CGFloat greenScale = RHGreen - RHGreen *sca;
CGFloat blueScale = RHBlue - RHBlue *sca;

self.textColor = [UIColor colorWithRed:redScale green:greenScale blue:blueScale alpha:1];
CGFloat fontScale = 1 +sca *0.3;
self.transform = CGAffineTransformMakeScale(fontScale, fontScale);

}

@end


#import<UIKit/UIKit.h>

UIKIT_EXTERN const  CGFloat RHRed;
UIKIT_EXTERN const  CGFloat RHGreen;
UIKIT_EXTERN const  CGFloat RHBlue;
UIKIT_EXTERN const  int Age;
UIKIT_EXTERN NSString *const Name;


#import <UIKit/UIKit.h>

const CGFloat RHRed = 0.4;
const CGFloat RHGreen = 0.6;
const CGFloat RHBlue = 0.7;
const int Age = 10;
NSString *const Name = @"刚刚好";


三:知识点总结:

1:用static const定义基本数据,或是字符串来代替宏定义来节省内存 ,利用父子控制器,先将子控制器添加到父控制器上,此时父控制器最子控制器有一个强引用,只要父控制器在,则子控制器就会存在,不会销毁。子控制器在,子控制器就会对其上的view有一个强引用,子控制器View就不会销毁

2:当有导航栏的时候,又有scrollView或是继承scrollView的子控件如tableView或是collecttionView,系统会自动为滚动视图增加额外的64滚动区域,解决可以采取两种方法:1:self.automaticallyAdjustsScrollViewInsets = NO;默认为YES,自动调整 2:设置contentInset属性,减少其额外的滚动区域。

3:scrollView设置:titleScroll.showsVerticalScrollIndicator = NO; titleScroll.showsHorizontalScrollIndicator = NO设置隐藏两个滚动条后,则scrollView的子控件中就没有了这两个子控件,就同UIView,可以用scrollView.subViews,得到添加到scrollView上的子控件。其中属性用weak修饰,但是依然对象没有销毁,是因为该view添加到了self.view上了,则self.view对该scrollView有一个强引用,所以其对象不会销毁

4: lable.text = [self.childViewControllers[i] title];不用再去定义数组装着标题,去设置lable的text,任何属性都会生成下划线的成员变量,set和get方法,set方法赋完值后,若想在其他方法中获得该值,则可调用其属性的get方法。

5:scrollView不能滚动:1:scrollView.enablescroll 2:没有设置scrollView的contentSize 3:

setContentOffset animated 该方法设置scrollView的偏移量,自动产生动画,不用再去搞UIView的动画 4:NSInteger:整数,去除小数点。NSUinteger,修饰的数是大于等于0的。

6:scrollView的代理方法:1:scrollViewDidScroll:监听滚动,实时调用 2:scrollViewDidEndDecelerating:减速停止方法,当手指松开的时候调用 3:

scrollViewDidEndScrollingAnimation 当scrollView所有滚动动画停止的时候调用

7:网易首页骨架的实现思路:1:利用父子控制器,先添加子控制器到父控制器中 2:分别创建titleScrollView和bottomScrollView,并为titleScrollView添加手势,为bottomScrollView设置代理,当点击顶部的滚动视图时,点击的标题居中,切换底部的视图,通过tap.view.tag;可获得点击lable的index,也就是底部滚动视图所需要偏移的位置。用setContentOffset animated,实现底部滚动视图的动画偏移,在滚动视图动画停止的代理方法中scrollViewDidEndScrollingAnimation,设置点击lable的自身居中显示,用点击lable的自身中心点x与屏幕中心点x作比较,小于0.则不让其偏移,若是大于顶部滚动视图的最大偏移量(contentSize.width - SCREENWIDTH),则让其偏移量为最大偏移量。改变自身居中显示后,还要偏移底部滚动视图,需要将底部相应控制器的从父控制器的数组中取出,将view添加到底部的scrollView上,此时要加判断, if ([newVC isViewLoaded]) return;如果子控制器已经加载过了,就返回,避免重复添加view。3:当滚动底部的滚动视图时,顶部的滚动视图也要同步显示,在减速停止方法中,再调用scrollViewDidEndScrollingAnimation即可 4:监听scrollView的实时滚动,从而设置lable的颜色和字体变化,先通过偏移量求出scrollView偏移到第几页CGFloat类型,再做条件过滤,如果if (scale < 0 || scale > self.childViewControllers.count -1) return;偏移的第几页也有可能出现负数,或是大于子控件个数。(其实也可以不去设置此过滤条件)第二个限制条件,不能用scrollView.subViews.count,原因是控制器的view是懒加载的,当滚动时,此时view还没有添加到scrollView上。左右滑动时,求出左右的index,从顶部滚动视图的子控件数组中取出lable,求出左右lable的比例,赋值给lable,重写lable比例的set方法,颜色的改变,按比例值改变,在原来颜色的比例上,增加或是减少。字体的变大,可以设置lable自身transform按比例进行缩放,比例值计算,在1的基础上加上按比例计算的缩放值。5:在didscrollView执行完毕后后,又会调用scrollViewDidEndScrollingAnimation,在此方法中,遍历顶部滚动视图的子控件,让别的lable恢复比例的默认值。6:设置默认新闻被选中,则相当于调用了scrollViewDidEndScrollingAnimation方法,并且在创建lable时,默认i==0时,设置比例值为1

8:全局常量的设置:定义头文件,在创建相同类名空文件作为.m文件,.m文件负责定义常量,.h负责extern引用常量。

9:注册tableView的表格,就不用再去判断创建cell了,在数据源方法中,直接从缓存池中取可重用cell就可以了, [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: