iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值