当前位置: 首页 > news >正文

NavigationBar 渐隐效果

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

考虑到继承UINavigationBar使用起来会非常不便,我们决定用Category来实现,首先定义我们的category:

@interface UINavigationBar (BackgroundColor)
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor;@end

实现:我们使用associatedObject将overlayView动态地绑定到UINavigationBar的instance上,当调用lt_setBackgroundColor的时候,我们只要更新这个overlayView就行啦~

@implementation UINavigationBar (BackgroundColor)static char overlayKey;- (UIView *)overlay
{    return objc_getAssociatedObject(self, &overlayKey);
}- (void)setOverlay:(UIView *)overlay
{objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{    if (!self.overlay) {[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];        // insert an overlay into the view hierarchyself.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, self.bounds.size.height + 20)];      self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;[self insertSubview:self.overlay atIndex:0];}    self.overlay.backgroundColor = backgroundColor;
}
@end

最后在scrollViewDidScroll中,我们就可以动态地修改UINavigationBar的backgroundColor了:

[self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat alpha=scrollView.contentOffset.y/90.0f>1.0f?1:scrollView.contentOffset.y/90.0f;

     [self.navigationController.navigationBar setBackgroundImage:[self getImageWithAlpha:alpha] forBarMetrics:UIBarMetricsDefault];

    }

#pragma handle image -mark

//合成图片

-(UIImage *)getImageWithAlpha:(CGFloat)alpha{

    

    UIColor *color=[UIColor colorWithRed:1 green:0 blue:0 alpha:alpha];

    CGSize colorSize=CGSizeMake(1, 1);

    UIGraphicsBeginImageContext(colorSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, color.CGColor);

    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));

    

    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}


转载于:https://my.oschina.net/gongxiao/blog/537848


http://www.taodudu.cc/news/show-854182.html

相关文章:

  • 代码随想录算法训练营第四十六天| 139.单词拆分,56. 携带矿石资源(第八期模拟笔试), 背包问题总结
  • MacApp自动化测试之Automator初体验
  • 嵌入式学习-M4的基本定时器
  • 基于spingboot,vue线上辅导班系统
  • Context Pattern上下文模式
  • 原子学习笔记7——FrameBuffer 应用编程
  • [数据结构]合并有序数组
  • spring BeanFactory加载xml配置文件示例
  • nginx location配置详解
  • 三星1TB硬盘MM804RS不识别加电磁头打盘敲盘异响不识别开盘恢复数据
  • Zookeeper Learning
  • 浑浑噩噩工作已多年
  • log4j+logback+slf4j+commons-logging的关系与调试
  • 使用 rem 实现 适配各种屏幕布局
  • 设计模式Adapter模式的五分钟
  • oracle的SCN和Checkpoint_Change#的关系
  • A problem while linking c++ to python
  • 几大基础算法
  • POPTEST学员就业面试题目!!!!!
  • 【自定义控件】自定义属性
  • 我的Git使用-资料查询,名博笔记
  • firefox-Developer开发者站点——关于Object.create()新方法的介绍
  • SharePoint 使用脚本为表单绑定事件
  • 解决Cannot change version of project facet Dynamic Web M
  • Github博客地址
  • c language compile process.
  • 阅读总结:如何在生产中成功运用Docker
  • [搜索]Trie树的实现
  • 老李分享:接口测试之jmeter
  • maven远程发布jar
  • GPRS模块AT呼叫控制命令
  • hadoop datanode启动失败
  • CentOS6.5下用yum安装 git .
  • 读博文学Android
  • javaWEB学习之filter
  • mysql字符集调整总结