个人主页中的背景图片和导航条透明度渐变实现
来源:互联网 发布:ios微信3d touch源码 编辑:程序博客网 时间:2024/06/14 19:38
实现的功能
- 上拉界面背景图片逐渐消失,下来界面背景图片放大(动态改变UIImageView的高度)
- 上拉导航条逐渐展示,下拉导航条逐渐隐藏(动态改变导航条的背景图片)
- 上拉导航条的标题逐渐显现,下拉导航条的标题逐渐隐藏(动态改变UILabel的textColor)
功能实现类似于QQ中的好友动态的顶部效果。
知识准备
导航条和导航条的子控件是不能直接通过alpha属性来隐藏的或者来改变透明度的,通常见到的透明有两种情况,一种是颜色,颜色是由RGB和alpha组成,另一种是图片,通过颜色也可以绘制一张图片,因颜色可以透明,所以图片也支持透明,例如png图片
界面搭建
使用Storyboard:
- self.view
- UITableView(全屏)
- UIView(容器View,用来作为背景图片、头像等控件的父控件, 高度固定,宽度=屏幕宽度)
- UIImageView(个人主页背景图片,frame = 容器view.frame)
- UIImageView(头像,宽度和高度固定)
- UIView(用于一组按钮的父控件,如相册、说说、个性化、消息等)
- UIButton(相册)
- UIButton (说说)
- UIButton (个性化)
- UIButton (消息)
#import "ProfileViewController.h"#define TopViewHeight 220@interface ProfileViewController () <UITableViewDelegate, UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (weak, nonatomic) IBOutlet UIView *topView;@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewheightConstraint;@end@implementation ProfileViewController- (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([self class])]; self.automaticallyAdjustsScrollViewInsets = NO; self.tableView.contentInset = UIEdgeInsetsMake(TopViewHeight, 0, 0, 0); [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]]; UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.text = @"个人主页"; titleLabel.textColor = [UIColor colorWithWhite:0 alpha:0]; [titleLabel sizeToFit]; self.navigationItem.titleView = titleLabel;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 1.动态的改变背景图片的高度(上拉(缩小)和下拉(放大)), 设置背景图片的内容模式为Aspect Fill, 设置父视图自动剪裁边框Clip To Bounds CGFloat offsetY = scrollView.contentOffset.y + TopViewHeight; CGFloat height = TopViewHeight - offsetY; self.topViewheightConstraint.constant = height; // 2.求透明度,根据透明颜色生成图片,设置导航条的透明背景图片 // 当前透明度 最大透明度 // -------- = ---------- // 当前偏移量 最大偏移量 CGFloat alpha = offsetY * 1 / (TopViewHeight - 64); NSLog(@"%f", alpha); UIColor *alphaColor = [UIColor colorWithWhite:1 alpha:alpha]; UIImage *alphaImage = [self imageWithColor:alphaColor]; [self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault]; // 3. 导航条标题: UILabel *titleLabel = (UILabel *)self.navigationItem.titleView; titleLabel.textColor = [UIColor colorWithWhite:0 alpha:alpha];}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 40;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class]) forIndexPath:indexPath]; cell.textLabel.text = [NSString stringWithFormat:@"mengday-%ld", indexPath.row]; return cell;}// 最好放到分类中- (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0, 0, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;}@end
实现效果如下:
示例Demo下载链接
0 0
- 个人主页中的背景图片和导航条透明度渐变实现
- 实现透明度渐变和颜色渐变
- 简单的代码实现上滑导航栏颜色和透明度渐变效果
- 设置导航条和状态栏透明度效果
- iOS开发实现导航栏透明度渐变效果
- code实现透明度渐变和颜色渐变的view
- code实现透明度渐变和颜色渐变的view
- code实现透明度渐变和颜色渐变的view
- code实现透明度渐变和颜色渐变的view
- 【iOS】View实现透明度渐变和颜色渐变
- iOS实现透明度渐变和颜色渐变的view
- JQuery实现背景图片渐变!
- JQuery实现背景图片渐变!
- 滚动视图上滑动改变导航条颜色和透明度
- java实现2张图按透明度和像素融合渐变过程
- JQuery Animate函数实现透明度和位置渐变的实例
- ios设置导航条背景图片
- 透明度渐变
- android之listview滑动与下拉刷新冲突 : listview未到顶部便出现下拉刷新
- codevs1993草地排水——第一次写网络流
- 使用autopilot部署OpenStack将不在Ubuntu 16.04中支持
- 【算法分析】排序算法
- java中设计模式-delegate(代理)
- 个人主页中的背景图片和导航条透明度渐变实现
- 职业平衡设计(未完成)
- android自定义View基础系列一(模仿365日历PC版加载动画)
- 如何应对IOS(苹果)强制启用 App Transport Security(ATS)安全功能
- mysql preparedStatement预编译
- nginx中的try_files指令解释
- java线程池
- python简单类
- #1273 – Unknown collation:‘utf8mb4_unicode_ci’错误的解决办法