根据滑动显隐状态栏的iOS实现
之前很多兄弟问如何实现类似于淘宝客户端搜索列表那种动态显隐的效果,这几天刚好有时间,就实现了几个例子搞一下,其实原理很简单,也参考了github上一位兄弟的实现.不多说,上代码
@interface D1ScrollingNaviBarViewController : UIViewController
//滑动隐藏,显示导航栏
-(void)followRollingScrollView:(UIView *)scrollView;
//用来处理导航栏下还有其他内容也需要跟随导航栏一起隐藏,显示的情况,类似于淘宝客户端的搜索
-(void)followRollingScrollView:(UIView *)scrollView secondAreaHeight:(CGFloat)secondAreaHeight;
@end
#define NAVI_BAR_HEIGHT 44.0
@interface D1ScrollingNaviBarViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) UIView *scrollView;
@property (retain, nonatomic)UIPanGestureRecognizer *panGesture;
@property (retain, nonatomic)UIView *overLay;
@property (assign, nonatomic)BOOL isHidden;
@property(nonatomic,assign)CGFloat secondAreaHeight;
@end @implementation D1ScrollingNaviBarViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.secondAreaHeight = 0.0;
}
return self;
} //设置跟随滚动的滑动试图
-(void)followRollingScrollView:(UIView *)scrollView
{
self.scrollView = scrollView; self.panGesture = [[UIPanGestureRecognizer alloc] init];
self.panGesture.delegate=self;
self.panGesture.minimumNumberOfTouches = 1;
[self.panGesture addTarget:self action:@selector(handlePanGesture:)];
[self.scrollView addGestureRecognizer:self.panGesture]; CGRect rect = self.navigationController.navigationBar.bounds;
self.overLay = [[UIView alloc] initWithFrame:rect];
self.overLay.alpha=0;
self.overLay.backgroundColor=self.navigationController.navigationBar.barTintColor;
[self.navigationController.navigationBar addSubview:self.overLay];
[self.navigationController.navigationBar bringSubviewToFront:self.overLay]; }
-(void)followRollingScrollView:(UIView *)scrollView secondAreaHeight:(CGFloat)secondAreaHeight
{
self.secondAreaHeight = secondAreaHeight;
[self followRollingScrollView:scrollView];
}
#pragma mark - 兼容其他手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
} #pragma mark - 手势调用函数
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGesture
{
CGPoint translation = [panGesture translationInView:[self.scrollView superview]];
//显示
if (translation.y >= 5) {
if (self.isHidden) { self.overLay.alpha=0;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect scrollViewFrame=self.scrollView.frame; navBarFrame.origin.y = 20;
scrollViewFrame.origin.y += self.secondAreaHeight+NAVI_BAR_HEIGHT;
scrollViewFrame.size.height -= self.secondAreaHeight+NAVI_BAR_HEIGHT; [UIView animateWithDuration:0.5 animations:^{
self.navigationController.navigationBar.frame = navBarFrame;
self.scrollView.frame=scrollViewFrame;
}];
self.isHidden= NO;
}
}
//隐藏
if (translation.y <= -20) {
if (!self.isHidden) {
CGRect frame =self.navigationController.navigationBar.frame;
CGRect scrollViewFrame=self.scrollView.frame;
frame.origin.y = -24;
scrollViewFrame.origin.y -= self.secondAreaHeight+NAVI_BAR_HEIGHT;
scrollViewFrame.size.height += self.secondAreaHeight+NAVI_BAR_HEIGHT; [UIView animateWithDuration:0.5 animations:^{
self.navigationController.navigationBar.frame = frame;
self.scrollView.frame=scrollViewFrame;
} completion:^(BOOL finished) {
self.overLay.alpha=1;
}];
self.isHidden=YES;
}
} } -(void)viewDidAppear:(BOOL)animated{
[self.navigationController.navigationBar bringSubviewToFront:self.overLay];
}
@end 要使用的类继承这个基类
//设置导航栏的背景色的两种方式
self.navigationController.navigationBar.barTintColor=[UIColor greenColor];//使用颜色 self.navigationController.navigationBar.barTintColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bg_h64"]];//使用图片 //44高度会被自动拉伸,所以需要注意图片的实际效果
self.navigationController.navigationBar.barTintColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:@"navi_bg_h44"]];//使用图片 //使用自动滑动显隐代码,这个50,是指紧挨着导航栏的要跟随显隐的区域的高度,如果没有这个区域,可以调用没有这个参数的接口,一样的
[self followRollingScrollView:self.view secondAreaHeight:50];
根据滑动显隐状态栏的iOS实现的更多相关文章
- C#中显/隐式实现接口及其访问方法
原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...
- NavigationBar的显隐和颜色设置
[self.navigationController setNavigationBarHidden:NO animated:NO]; self.navigationController.navigat ...
- JS/jquery实现鼠标控制页面元素显隐
最近网站要上一个活动广告横幅,当用户鼠标划过时显隐二维码.像这种鼠标事件控制页面元素显隐的情况,码农们会经常遇到,可以通过javascript或jquery代码实现,下面就几种常见需求一起归纳一下. ...
- 盒子显隐,伪类边框,盒子阴影,2d平面形变
-盒子显隐 显隐的盒子尽量不影响其他盒子的布局 display:none; 消失的时候不占位置,显示的时候占位 opacity:0-1; 盒子透明度 overflow: hidden; 超出部分隐藏 ...
- day50 盒子显隐2D形变
复习 1.浮动布局 解决block盒子同行显示 => 不完全脱离文档流 => 不再撑开父级高度 脱离文档流: 不在页面中占位(显示层次高于文档流) 不完全: 可以通过清浮动操作, 让子级重 ...
- Python12/11--盒子的显隐/布局/z-index/流式布局思想
1.盒子的显隐 display:none 在页面中不占位,采用定位布局后,显示隐藏都不会影响其他标签,不需要用动画处理时,一般用这个 opacoity : 0 在页面中占位,采 ...
- jq svg 修改image的xmlns:xlink及图片的显隐
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变
字体图标 ''' fa框架: http://fontawesome.dashgame.com/ 下载 => 引入css文件 引入字体图标库 <link rel="styleshe ...
- 浏览器页面的显隐对js的setInterval()执行所产生的bug
前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...
随机推荐
- 15.Linux安装DHCP服务为虚拟机分配IP
参考博客:http://www.jb51.net/article/31607.htm $ rpm -ql dhcp #检查是否安装dhcp $ yum -y install dhcp* ...
- SecureCRT配色
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单的说是Windows下登录UNIX或Linux服务器主机的软件.SecureCRT支持SSH,同时支持Telnet和rlog ...
- 深入浅出设计模式——代理模式(Proxy Pattern)
模式动机在某些情况下,一个客户不想或者不能直接引用一个对象,此时可以通过一个称之为“代理”的第三者来实现间接引用.代理对象可以在客户端和目标对象之间起到中介的作用,并且可以通过代理对象去掉客户不能看到 ...
- python成长之路【第七篇】:面向对象
概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向对象三大特性 面向 ...
- JavaScript笔记三两个
JavaScript定义变量: var num;变量的类型不知道,当给num赋值后,根据值类型来确定类型. var num = 3; //数值类型 var str = "hello,worl ...
- [cocos2d-x]OPENGL ES支持的像素格式
OPENGL ES最多支持32位颜色值. 支持的像素格式有以下几种: 客户端格式 GL格式 GL数据类型 字节数 RGBA8888 GL_RGBA GL_UNSIGNED_BYTE 4 RGB888 ...
- attr和prop
<div class="content-item active"> <table class="table"> <thead> ...
- linux下多路复用模型之Select模型
Linux关于并发网络分为Apache模型(Process per Connection (进程连接) ) 和TPC , 还有select模型,以及poll模型(一般是Epoll模型) Select模 ...
- java_easyui体系之DataGrid(4)[转]
一:简介 在前面DataGrid(3)的基础上添加后台的实现.本来是想只搭建前台页面的.后台不写.现在觉得还是都实现好点.从真实情况出发.后台用的ssh. 1. 新增冻结列功能. 2. 实现界面的添加 ...
- UE3:SkeletalMesh的绘制流程
[目标] SkeletalMesh的绘制流程 [思路] 1 顶点缓冲流 静态数据流向 动态数据流向(紫红色箭头) 2 FGPUSkinVertexFactory.ShaderDataType.Bone ...