iOS开发渐变导航栏终极版

释放双眼,带上耳机,听听看~!

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

以上是博主写的渐变导航栏的进化之路,上一篇博客最后说明了升级版中的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文章

iOS开发使用IJKPlayer时,关于需求要边下边播的缓存功能,退回来后播放缓存不再耗流量

2021-2-2 21:09:52

iOS文章

iOS开发加密key的SHA256和MD5加密

2021-2-2 22:24:55

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索