UITableView头部ImageView下拉放大效果,导航栏透明渐变
2016-03-04 11:15
519 查看
为了方便自己记忆,赖的详细写了,先贴上代码,有空在详细解读。
这里导航栏透明用的是这位大神的http://tech.glowing.com/cn/change-uinavigationbar-backgroundcolor-dynamically/
这里导航栏透明用的是这位大神的http://tech.glowing.com/cn/change-uinavigationbar-backgroundcolor-dynamically/
#import "ViewController.h" #import "UINavigationBar+Awesome.h" #define ImageWidth [[UIScreen mainScreen] bounds].size.width static CGFloat imageH = 200; static CGFloat navH = 64; @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (strong, nonatomic) UITableView *tableView; @property (nonatomic, strong) UIImageView *headerView; @property (nonatomic, strong) UIImage *shadowImage; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:nil]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:nil]; self.automaticallyAdjustsScrollViewInsets = NO; self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.contentInset = UIEdgeInsetsMake(imageH, 0, 0, 0); [self.view addSubview:self.tableView]; self.headerView = [[UIImageView alloc]init]; self.headerView.frame = CGRectMake(0, -imageH, ImageWidth, imageH); self.headerView.image = [UIImage imageNamed:@"IMG_0106.JPG"]; self.headerView.contentMode = UIViewContentModeScaleAspectFill; [self.tableView addSubview:self.headerView]; [self.tableView insertSubview:self.headerView atIndex:0]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.shadowImage = self.navigationController.navigationBar.shadowImage; [self.navigationController.navigationBar setShadowImage:[UIImage new]]; CGFloat offsetY = self.tableView.contentOffset.y; [self changeNavAlphaWithConnentOffset:offsetY]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController.navigationBar lt_reset]; self.navigationController.navigationBar.shadowImage = self.shadowImage; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 50; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"XXXX"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIViewController *vc = [[UIViewController alloc]init]; vc.view.backgroundColor = [UIColor whiteColor]; [self.navigationController pushViewController:vc animated:YES]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; NSLog(@"%f",offsetY); if (offsetY < -imageH) { NSLog(@"开始改变"); CGRect f = self.headerView.frame; f.origin.y = offsetY; f.size.height = -offsetY; self.headerView.frame = f; } [self changeNavAlphaWithConnentOffset:offsetY]; } -(void)changeNavAlphaWithConnentOffset:(CGFloat)offsetY { UIColor *color = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1]; if (offsetY > -navH * 2 ) { NSLog(@"渐渐不透明"); CGFloat alpha = MIN(1, 1 - ((-navH * 2 + navH - offsetY) / navH)); [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]]; self.title = @"个人主页"; } else { NSLog(@"渐渐透明"); [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]]; self.title = @""; } }
相关文章推荐
- ural 2071. Juice Cocktails
- ural 2071. Juice Cocktails
- Kendo UI
- 【实战Java高并发程序设计 7】让线程之间互相帮助--SynchronousQueue的实现
- iOS9新特性——堆叠视图UIStackView(2)
- IOS 多个UIImageView 加载高清大图时内存管理
- EasyUI Layout 布局
- UI动画架构(上下滚动,里面View可以实现动画<透明,移动,缩放,变色>l)
- iOS9新特性——堆叠视图UIStackView
- easyui datagrid 获取 title
- 1099. Build A Binary Search Tree (30)
- [LeetCode]Increasing Triplet Subsequence
- 让你明白response.sendRedirect()与request.getRequestDispatcher().forward()区别
- Easyui CSS式样重写
- IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- android requestWindowFeature使用详解
- 从源码分析 对 非UI线程不允许访问UI元素的 理解
- UIAutomator remote debug方法
- Uiautomator使用
- EasyUI 的各种提示框消息框配置