博主以前发过一篇渐变导航条的,地址: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的点击下载:下载