iOS开发 - 渐变导航栏终极版

渐变导航栏初级版本:http://blog.csdn.net/codingfire/article/details/51604098
渐变导航栏升级版本:http://blog.csdn.net/codingfire/article/details/53705318

以上是博主写的渐变导航栏的进化之路,上一篇博客最后说明了升级版中的bug,在这一版中对这一bug进行了修复,其实很简单,就是增加了一个新的变量来给一个全局的透明度,在上下滚动时改变这个全局的透明度,然后判断透明度的边界,下面来看看代码吧:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor=[UIColor whiteColor];

    lhAlpha = 1;
}
- (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);
            if (lhAlpha >= 0) {
                lhAlpha = lhAlpha - 0.025f;
            }
            else
            {
                lhAlpha = 0;
            }
            self.navigationController.navigationBar.alpha = lhAlpha;

//            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);
            if (lhAlpha <= 1) {
                //这里的0.025自己可以调整,有的上下滚动透明度变化一个快一个慢,切记不要让向下的小于向上的,否则滚动到顶端导航栏是透明的
                lhAlpha = lhAlpha + 0.025f;
            }
            else
            {
                lhAlpha = 1;
            }
            self.navigationController.navigationBar.alpha = lhAlpha;
            //            self.navigationController.navigationBar.alpha = (stopPosition - currentPostion)/200;
        }

    }

}

Demo的下载地址:下载地址

建议先看一下前面两篇,这样会增加理解,实现不难,主要是一个思路。

你可能感兴趣的:(iOS日常开发)