实现 iOS 头部拉伸效果
来源:互联网 发布:法国房补 知乎 编辑:程序博客网 时间:2024/06/06 07:30
主要涉及到导航栏透明度、图片拉伸、列表头部等。
- 导航栏透明度的实现。
- 列表拖动距离的监听,及图片放大的实现。
导航透明度的设置
添加系统导航栏的Category实现
声明部分:
@interface UINavigationBar (BackgroundColor)- (void)lt_setBackgroundColor:(UIColor *)color;@end
实现部分:
#import <objc/runtime.h>@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 *)color{ if (!self.overlay) { [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; // insert an overlay into the view hierarchy self.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 = color;}@end
监听列表拖动及实现图片放大
主要是监听滚动的距离(scrollViewDidScroll:方法)
#import "StretchViewController.h"#import "UINavigationBar+BackgroundColor.h"// 背景图片的宽高比例#define ratio 0.8@interface StretchViewController () <UITableViewDelegate, UITableViewDataSource>// 可放大的背景图片@property (nonatomic, strong) UIImageView *bgView;// 记录原始大小@property (assign) CGRect originalFrame;@property (nonatomic, strong) UITableView *tableView;@end@implementation StretchViewController- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //[self.navigationController setNavigationBarHidden:YES animated:animated]; //self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //self.navigationController.navigationBar.barTintColor = [UIColor clearColor]; //self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; // 设置导航栏底部分割线为透明 [self.navigationController.navigationBar setShadowImage:[UIImage new]];}- (void)viewDidLoad { [super viewDidLoad]; // 设置全透明 [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0]]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor lightGrayColor]; self.bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width*ratio)]; self.bgView.image = [UIImage imageNamed:@"bg-mine"]; self.originalFrame = self.bgView.frame; [self.view addSubview:self.bgView]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain]; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.showsVerticalScrollIndicator = NO; self.tableView.delegate = self; self.tableView.dataSource = self; // 1. contentInset //table.contentInset = UIEdgeInsetsMake(160, 0, 0, 0); // 2. heatView UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 160)]; headView.backgroundColor = [UIColor clearColor]; self.tableView.tableHeaderView = headView; [self.view addSubview:self.tableView];}- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"]; } cell.textLabel.text = @"测试数据"; return cell;}- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat yOffset = scrollView.contentOffset.y; // 向上滑动,offset是增加的;向下滑动,是减少的 if (yOffset < 160) { // 当滑动到导航栏底部时 CGFloat colorAlpha = yOffset/160;// self.navigationController.navigationBar.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:colorAlpha]; [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:colorAlpha]]; } else { // 超过导航栏底部了 [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]]; } // 往上滑动效果、处理放大效果 if (yOffset > 0) { self.bgView.frame = ({ CGRect frame = self.bgView.frame; frame.origin.y = self.originalFrame.origin.y - yOffset; frame; }); } else { // 往下移动,放大效果 self.bgView.frame = ({ CGRect frame = self.originalFrame; frame.size.height = self.originalFrame.size.height - yOffset; frame.size.width = frame.size.height/ratio; // frame.origin.x = self.originalFrame.origin.x - (frame.size.width - self.originalFrame.size.width)/2; frame; }); }}@end
以上是对系统原生的导航栏进行透明度设置。
也可进行自定义视图设置为导航栏效果如下:
阅读全文
0 0
- 实现 iOS 头部拉伸效果
- IOS开发UI篇--使用UICollectionView实现一个列表头部拉伸效果的案例
- iOS仿keep5.3.0版本健身等级界面实现UITableView头部图片下拉纵向拉伸效果
- iOS Scrollview 的头部view的拉伸伸缩效果
- iOS 实现UICollectionView头部悬停效果
- UIUITableView头部图片拉伸放大效果
- iOS开发010 tableView头部拉伸效果(类似QQ空间)
- 使用UICollectionView实现一个列表头部拉伸效果(下拉放大)(OC 和 Swift)
- iOS-tableview顶部拉伸效果(头像拉伸)
- IOS-UIButton和UIImageView中Image拉伸效果的实现
- iOS 实现类似安卓.9.png图片拉伸效果
- iOS中气泡对话框拉伸效果视图的实现
- js实现ios头部导航条模糊效果
- QQ空间头部图片可拉伸的ListView效果
- Swift实现视图拉伸效果
- ListView实现头部悬停效果
- iOS --代码实现拉伸图片
- ios 图片拉伸 产生气泡效果
- 怎么把系统桌面设置到其他盘
- 遇到一个Mysql死锁问题,记录下
- JS /Jquery脚本去除 空格 回车 换行
- 1900页Python系列PPT分享二:Python序列(列表、元组、字典、集合)(154页)
- 我看单例模式
- 实现 iOS 头部拉伸效果
- Ealsticsearch 5.x Mapping(映射)
- 一位资深程序员大牛给予Java初学者的学习路线建议
- BZOJ1663: [Usaco2006 Open]赶集
- mysql 主从复制配置
- MFC如何在主线程中向view类发消息或是其他类向主框架类发消息
- Warning: require(/www/101/eduask_net/ThinkPHP/Library/Think/Think.class.php): failed to open stream:
- python的函数
- spring+maven项目连接数据库要配置以下内容