iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
来源:互联网 发布:中国2015gdp数据 编辑:程序博客网 时间:2024/05/16 09:42
博主以前发过一篇渐变导航条的,地址:http://blog.csdn.net/codingfire/article/details/51604098
但是在中部滚动的时候不能实现渐变,类似的好的效果在百度贴吧里出现过,所以博主对那个Demo进行了改进,看下效果:
这里加上了对向上向下和改变方向时的位置的判断,下面看下代码:
#import "ViewController.h"@interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>{ UIImageView *barImageView; UITableView *myTableView; float _lastPosition; float currentPostion; float stopPosition;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor=[UIColor whiteColor]; // [self.navigationController.navigationBar setBackgroundImage:[UIImage new] // forBarMetrics:UIBarMetricsDefault]; // self.navigationController.navigationBar.shadowImage = [UIImage new]; _lastPosition = 0; stopPosition = 0; [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"first"]; [[NSUserDefaults standardUserDefaults]synchronize]; self.navigationController.navigationBar.barTintColor=[UIColor orangeColor]; self.title=@"This is my title!"; self.navigationController.navigationBar.tintColor = [UIColor blackColor]; myTableView=[[UITableView alloc]initWithFrame:self.view.bounds]; myTableView.delegate=self; myTableView.dataSource=self; [self.view addSubview:myTableView]; UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 270)]; imageView.image=[UIImage imageNamed:@"1.png"]; myTableView.tableHeaderView=imageView; barImageView = self.navigationController.navigationBar.subviews.firstObject; NSString *str=@"122222";}#pragma mark - UITableViewDelaget-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 40;}-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 44;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row]; return cell;}- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ [NSObject cancelPreviousPerformRequestsWithTarget:self]; stopPosition = currentPostion + 64; NSLog(@"滑动停止:%f",stopPosition);}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ currentPostion = scrollView.contentOffset.y; if (currentPostion > 0) { if (currentPostion - _lastPosition >= 0) { if ([[NSUserDefaults standardUserDefaults]objectForKey:@"first"]!=nil) { [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"first"]; [[NSUserDefaults standardUserDefaults]synchronize];// [NSObject cancelPreviousPerformRequestsWithTarget:self];// [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.00001]; [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"second"]; [[NSUserDefaults standardUserDefaults]synchronize]; stopPosition = currentPostion + 64; } _lastPosition = currentPostion; NSLog(@"ScrollUp now current:%f last:%f stop:%f",currentPostion,_lastPosition,stopPosition); self.navigationController.navigationBar.alpha = 1 - currentPostion / 400; } else { if ([[NSUserDefaults standardUserDefaults]objectForKey:@"second"]!=nil) { [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"second"]; [[NSUserDefaults standardUserDefaults]synchronize];// [NSObject cancelPreviousPerformRequestsWithTarget:self];// [self performSelector:@selector(scrollViewDidEndScrollingAnimation:) withObject:nil afterDelay:0.00001]; [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"first"]; [[NSUserDefaults standardUserDefaults]synchronize]; stopPosition = currentPostion + 64; } _lastPosition = currentPostion; NSLog(@"ScrollDown now current:%f last:%f stop:%f",currentPostion,_lastPosition,stopPosition); self.navigationController.navigationBar.alpha = (stopPosition - currentPostion)/200; } }}
这里有一个关于滚动停止的判断,但是博主注释了代码,在下一篇博客中,会针对滚动停止,和改变方向,以及滚动未停止时改变方向的情况作说明。
代码很简单,需要Demo的点击下载:下载
0 0
- iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
- 判断滚动条的滚动方向
- iOS 判断滚动视图的滚动方向
- iOS开发 - ScrollView滚动时怎么判断滚动停止及滚动的方向
- ListView滚动方向和滚动位置的探索
- iOS之判断滚动视图的滚动方向
- ios开发-判断手机方向改变
- ios开发屏幕旋转锁定时判断设备的方向
- [ios]ios开发之检测UIScrollView的滚动方向 【转】
- js判断滚动条滚动方向
- js判断滚动条滚动方向
- js判断滚动条滚动方向
- 制造垂直方向的滚动条
- bootstrap 使用row时出现的左右方向滚动条
- ios开发之检测UIScrollView的滚动方向
- Android 如何判断ListView的滚动方向
- 如何判断tableView的滚动方向
- 判断页面鼠标滚动的方向
- 手机淘宝搜索排名的那些规则你知道多少?
- mysql数据库优化(一)
- poj 1182_食物链_并查集
- STL map简洁教程
- [高精度 递归] BZOJ 2930 [Poi1999]梦游者 Sleepwalker
- iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
- java 中的 sql 单引号双引号拼接变量
- Android软键盘监听KeyboardWatcher
- 最大和子矩阵
- 斐波那契数列的三种算法
- 【SSLGZ 1762】工厂的烦恼
- 6.7 Swift闭包捕获值
- Linux定时任务
- js调试