之前很多兄弟问如何实现类似于淘宝客户端搜索列表那种动态显隐的效果,这几天刚好有时间,就实现了几个例子搞一下,其实原理很简单,也参考了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实现的更多相关文章

  1. C#中显/隐式实现接口及其访问方法

    原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...

  2. NavigationBar的显隐和颜色设置

    [self.navigationController setNavigationBarHidden:NO animated:NO]; self.navigationController.navigat ...

  3. JS/jquery实现鼠标控制页面元素显隐

    最近网站要上一个活动广告横幅,当用户鼠标划过时显隐二维码.像这种鼠标事件控制页面元素显隐的情况,码农们会经常遇到,可以通过javascript或jquery代码实现,下面就几种常见需求一起归纳一下. ...

  4. 盒子显隐,伪类边框,盒子阴影,2d平面形变

    -盒子显隐 显隐的盒子尽量不影响其他盒子的布局 display:none; 消失的时候不占位置,显示的时候占位 opacity:0-1; 盒子透明度 overflow: hidden; 超出部分隐藏 ...

  5. day50 盒子显隐2D形变

    复习 1.浮动布局 解决block盒子同行显示 => 不完全脱离文档流 => 不再撑开父级高度 脱离文档流: 不在页面中占位(显示层次高于文档流) 不完全: 可以通过清浮动操作, 让子级重 ...

  6. Python12/11--盒子的显隐/布局/z-index/流式布局思想

    1.盒子的显隐 display:none      在页面中不占位,采用定位布局后,显示隐藏都不会影响其他标签,不需要用动画处理时,一般用这个 opacoity : 0        在页面中占位,采 ...

  7. jq svg 修改image的xmlns:xlink及图片的显隐

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变

    字体图标 ''' fa框架: http://fontawesome.dashgame.com/ 下载 => 引入css文件 引入字体图标库 <link rel="styleshe ...

  9. 浏览器页面的显隐对js的setInterval()执行所产生的bug

    前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...

随机推荐

  1. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序处理并发

    这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第十篇:为ASP.NET MVC应用程序 ...

  2. Kanzi Studio中的概念

    Kanzi Studio是Kanzi的UI编辑器,功能非常强大.在使用Kanzi Stadio之前,首先要先熟悉编辑器中的概念. Kanzi Studio中主要分project窗格,property窗 ...

  3. c++ 指针常量,常量指针

    当const遇到指针 一般来说,const修饰指针可以分为下面的集中情况. 描述 例子 含义 备注 const在*的左边 const int *b=&a; int const *b=& ...

  4. 通过配置的方式Autofac 《第三篇》

    一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...

  5. 深入浅出设计模式——访问者模式(Visitor Pattern)

    模式动机 对于系统中的某些对象,它们存储在同一个集合中,且具有不同的类型,而且对于该集合中的对象,可以接受一类称为访问者的对象来访问,而且不同的访问者其访问方式有所不同,访问者模式为解决这类问题而诞生 ...

  6. [Mysql] mysql临时表corrupt

    问题描述 机房临时硬件问题, 机器掉电. 恢复后, 重启mysql进程, 结果出现core dump. 而且一直循环, 无法终止, 只能kill掉. backtrace如下. # service my ...

  7. 关于Python中输出中文的一点疑问

    #encoding=gb2312 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.re ...

  8. StringBuffer delete

    描述 java.lang.StringBuffer.delete() 方法将删除这个序列的一个子字符串中的字符. 子字符串的开始在指定的start和延伸处的字符索引end - 1或结束的序列,如果不存 ...

  9. jquery 返回上一页 ,关闭js代码

    1.关闭功能: $(“#guanbi”).live("click",function()){ window.close(); } 2.返回上一页: <a href=" ...

  10. 数据库 基础篇3(mysql语法)

    4 数据库管理(接上篇) 4.1 查询所有数据库 mysql> show databases; +--------------------+ | Database           | +-- ...