/** 屏幕宽高 */
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,assign) CGFloat lastContentOffset;
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"导航栏动态高度切换";
[self.view addSubview:self.tableView];
[self.view addSubview:self.imageView];
}
#pragma mark - lazy
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.estimatedRowHeight = 0.0;
_tableView.sectionHeaderHeight = 0.0;
_tableView.sectionFooterHeight = 0.0;
_tableView.estimatedSectionHeaderHeight = 0.0;
_tableView.estimatedSectionFooterHeight = 0.0;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableView class])];
}
return _tableView;
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] init];
_imageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-60, 400, 60, 60);
_imageView.image = [UIImage imageNamed:@"zrx4.jpg"];
_imageView.layer.cornerRadius = 15;
_imageView.layer.masksToBounds = YES;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
_imageView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
}
return _imageView;
}
#pragma mark - lazyLoding
- (NSArray *)dataSource {
if (!_dataSource) {
_dataSource = [UIFont familyNames];
}
return _dataSource;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
//给cell添加动画
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//设置Cell的动画效果为3D效果
//设置y的初始值为0.1;
cell.layer.transform = CATransform3DMakeScale(0.1, 1, 1);
//x和y的最终值为1
[UIView animateWithDuration:0.25f animations:^{
cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableView class])];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = self.dataSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" == %@",self.dataSource[indexPath.row]);
}
#pragma mark - //开始滑动的时候记录位置
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_lastContentOffset = scrollView.contentOffset.y;
[UIView animateWithDuration:0.25 animations:^{
self.imageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-15, 400, 60, 60);
}];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[UIView animateWithDuration:0.25 animations:^{
self.imageView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-60, 400, 60, 60);
}];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > _lastContentOffset) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
self.tableView.frame = CGRectMake(0, 20, SCREEN_W, SCREEN_H - 20);
self.view.backgroundColor = [UIColor whiteColor];
} else {
[self.navigationController setNavigationBarHidden:NO animated:YES];
self.tableView.frame = CGRectMake(0, 64, SCREEN_W, SCREEN_H - 64);
self.view.backgroundColor = [UIColor whiteColor];
}
}
@end