一、介绍:

在前面已经介绍了一种条件悬浮框,使用的是tableView的Plain分组样式实现的,因为这是tableView本身就具备的功能,分组悬浮效果。这次我来介绍第二种更加简单的方法,采用两个ScrollView来实现。

二、实现技术:

(1)两个ScrollView,一个是左右滚动,成为内容视图,另一个是上下滚动,作为容器视图;

(2) 创建头视图,头视图中有banner图和条件筛选框,标记banner图的高;

(3)合理设置上下滚动的容器视图的frame,它承载头视图和内容视图,不过需要禁止它的弹簧效果,实现悬浮框功能。

三、代码如下:

HeadView.h

//  HeadView.h
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import <UIKit/UIKit.h> @interface HeadView : UIView
/**
* 类方法创建头视图
*/
+(instancetype)createHeadView:(CGRect)frame;
@property (strong,nonatomic)UIView *animationView; //下划线动画视图
@end

HeadView.m

//  HeadView.m
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import "HeadView.h"
#import "ViewController.h" #define padding 10
#define Width [[UIScreen mainScreen] bounds].size.width
#define Height [[UIScreen mainScreen] bounds].size.height @interface HeadView ()
@property (strong,nonatomic)UIImageView *bannerView; //banner图
@property (strong,nonatomic)NSMutableArray *buttonNames; //标题按钮数组
@end @implementation HeadView /**
* 懒加载
*/
-(NSMutableArray *)buttonNames{
if (!_buttonNames) {
_buttonNames = [NSMutableArray arrayWithObjects:@"运动",@"电玩",@"养生",@"娱乐",nil];
}
return _buttonNames;
} -(UIImageView *)bannerView{
if (!_bannerView) { _bannerView = [[UIImageView alloc]initWithFrame:CGRectMake(,,Width,)];
_bannerView.image = [UIImage imageNamed:@"demo"];
}
return _bannerView;
} -(UIView *)animationView{ if (!_animationView) {
_animationView = [[UIView alloc]initWithFrame:CGRectMake(, CGRectGetMaxY(self.bannerView.frame)++padding/, Width/, padding/)];
_animationView.backgroundColor = [UIColor redColor];
}
return _animationView;
} //重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) { //1.创建轮播图
[self addSubview:self.bannerView]; //2.创建标题
for (int i=; i<; i++) { //按钮
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:self.buttonNames[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
btn.backgroundColor = [UIColor whiteColor];
btn.titleLabel.font = [UIFont systemFontOfSize:]; //分割线
UILabel *line = [[UILabel alloc]init];
line.tag = i;
line.backgroundColor = [UIColor lightGrayColor]; //添加
[self addSubview:line];
[self addSubview:btn];
} //3.创建动画视图
[self addSubview:self.animationView];
}
return self;
} //类方法
+(instancetype)createHeadView:(CGRect)frame{ HeadView *headView = [[self alloc]initWithFrame:frame];
return headView;
} #pragma mark - 按钮被点击
//点击按钮
-(void)buttonClick:(UIButton *)btn{ [UIView animateWithDuration:0.2 animations:^{ //移动动画视图的frame
CGRect frame = self.animationView.frame;
frame = CGRectMake(Width/*btn.tag,frame.origin.y,frame.size.width,frame.size.height);
self.animationView.frame = frame; //移动scrollView的偏移位置
ViewController *currentVC = (ViewController*)[self viewController];
[currentVC.contentView setContentOffset:CGPointMake(Width*btn.tag, ) animated:NO];
}];
} //获取当前视图所在的控制器
- (UIViewController*)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
} //设置frame
-(void)layoutSubviews{
[super layoutSubviews]; for (UIView *mysubView in self.subviews) {
if ([mysubView isKindOfClass:[UIButton class]]) { //按钮
UIButton *button = (UIButton *)mysubView;
CGFloat X = (Width/) * button.tag;
button.frame = CGRectMake(X, CGRectGetMaxY(self.bannerView.frame)+padding/, (Width/), );
}
if ([mysubView isKindOfClass:[UILabel class]]) { //分割线
UILabel *line = (UILabel *)mysubView;
CGFloat X = (Width/) * line.tag - 0.5;
line.frame = CGRectMake(X, CGRectGetMaxY(self.bannerView.frame)+1.5*padding,,);
}
}
} @end

ContentView.h

//  ContentView.h
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import <UIKit/UIKit.h> @interface ContentView : UIScrollView //类方法创建
+(instancetype)shareWithFrame:(CGRect)frame;
@end

ContentView.m

//  ContentView.m
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import "ContentView.h" #define XYQColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define XYQRandomColor XYQColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define SCREEN_W [[UIScreen mainScreen] bounds].size.width
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height @implementation ContentView //初始化
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) { //放tableView
for (int i=; i<; i++) {
UIImageView *imgViw = [[UIImageView alloc]init];
imgViw.backgroundColor = XYQRandomColor;
imgViw.frame = CGRectMake(SCREEN_W*i, , SCREEN_W, SCREEN_H-);
[self addSubview:imgViw];
}
}
return self;
} //类方法创建
+(instancetype)shareWithFrame:(CGRect)frame{ ContentView *contentView = [[self alloc]initWithFrame:frame];
contentView.contentSize = CGSizeMake(SCREEN_W*,frame.size.height);
contentView.pagingEnabled = YES;
contentView.bounces = NO;
return contentView;
}
@end

ViewController.h

//  ViewController.h
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import <UIKit/UIKit.h>
#import "ContentView.h" @interface ViewController : UIViewController /**
* 内容视图
*/
@property (strong,nonatomic)ContentView *contentView; @end

ViewController.m

//  ViewController.m
// SuspensionViewDemo
//
// Created by 夏远全 on 16/11/25.
// Copyright © 2016年 广州市东德网络科技有限公司. All rights reserved.
// #import "ViewController.h"
#import "HeadView.h" #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height @interface ViewController ()<UIScrollViewDelegate>{ UIScrollView *_containView; //大容器视图(存放头视图和内容视图)
}
/**
* 头视图
*/
@property (strong,nonatomic)HeadView *headView; @end @implementation ViewController /**
* 给定头视图的高
*/
static NSInteger const headHeight = ; - (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]]; //创建控件
[self setupControl];
} //创建控件
-(void)setupControl{ //1.设置容器视图
_containView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_containView.tag = -;
_containView.delegate = self;
_containView.bounces = NO; //禁止它的弹簧效果
_containView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+);
[self.view addSubview:_containView]; //2.创建头视图
_headView = [HeadView createHeadView:CGRectMake(,,SCREEN_WIDTH,headHeight)];
[_containView addSubview:_headView]; //3.创建内容视图
_contentView = [ContentView shareWithFrame:CGRectMake(, headHeight, SCREEN_WIDTH, SCREEN_HEIGHT-)];
_contentView.tag = -;
_contentView.delegate = self;
[_containView addSubview:_contentView];
} #pragma mark - <UIScrollViewDeleagte>
//设置下划动画视图的frame
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView.tag == -) { //横向的滚动内容视图
NSInteger page = scrollView.contentOffset.x/SCREEN_WIDTH;
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = self.headView.animationView.frame;
frame = CGRectMake(SCREEN_WIDTH/*page,frame.origin.y,frame.size.width,frame.size.height);
self.headView.animationView.frame = frame;
}];
}
}
@end

四、演示截图:(上拉到顶就悬浮了不能在上拉,下拉可以继续),点击按钮来回切换tableView

 

iOS: 悬浮的条件筛选框使用二的更多相关文章

  1. iOS: 悬浮的条件筛选下拉框的使用

    1.介绍 app中条件筛选视图是很常用的功能,一般它搭配着tableView的表头悬浮滚动使用,点击按钮时,就会弹出下拉框显示条件,选择一个条件后,下拉框自动隐藏. 2.效果图如下 从中间点击弹出,然 ...

  2. iOS开发-UI 从入门到精通(二)

    iOS开发-UI 从入门到精通(二)是对 iOS开发-UI 从入门到精通(一)知识点的巩固,主要以习题练习为主,增强实战经验,为以后做开发打下坚实的基础! ※开发环境和注意事项: 1.前期iOS-UI ...

  3. 织梦CMS实现多条件筛选功能

    用织梦实现筛选的功能,其实主要就是运用到了织梦的高级搜索功能,然后用ajax去post替换掉本来的结果就可以了. 其实筛选的话,主要有两个问题需要解决,一个是前台的筛选实现,一个是后台根据前台的点击, ...

  4. 记录下url拼接的多条件筛选js

    本着为提高工作效率百度或者google这些代码发现拿过来的都不好用,然后自己写了个,写的一般但记录下以后再优化 <html> <head> <script> $(f ...

  5. python基础一 -------如何在列表字典集合中根据条件筛选数据

    如何在列表字典集合中根据条件筛选数据 一:列表 先随机生成一个列表,过滤掉负数 1,普通for循环迭代判断 2,filter()函数判断,filter(函数,list|tuple|string) fi ...

  6. mxonline实战8,机构列表分页功能,以及按条件筛选功能

    对应github地址:列表分页和按条件筛选     一. 列表分页   1. pip install django-pure-pagination   2. settings.py中 install ...

  7. js前端 多条件筛选查询

    一.前言 在做项目中,遇到多条件筛选案例.实现完成以后,我将我做的代码分享在这里,希望可以帮助到其他朋友. 二.效果截图 三.实现代码 首先我先类型.类别.职位分成三块来处理,如果传到服务器端的话,就 ...

  8. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  9. iOS开发UI篇—Quartz2D简单使用(二)

    iOS开发UI篇—Quartz2D简单使用(二) 一.画文字 代码: // // YYtextview.m // 04-写文字 // // Created by 孔医己 on 14-6-10. // ...

随机推荐

  1. zabbix企业级监控概述和部署

    官方网站:http://www.zabbix.com/ zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 zabbix能监视各种网络参数,保证服务器系统的安全 ...

  2. 《Java核心技术卷二》笔记(一)流与文件

    一.流的概念 在Java中,可以提供字节序列的对象,或者可以接收字节序列的对象,都可以抽象成流.系统中的文件,网络,内存这些设备都可以读入或者写入字节,自然也可以用流的方式来操作.能向程序中提供字节序 ...

  3. 2016.04.27,英语,《Vocabulary Builder》Unit 19

    bio, comes from the Greek word for 'life'. biosphere ['baɪoʊsfɪr] n. 生物圈: biology [baɪ'ɑːlədʒi] n. 生 ...

  4. NVPerfHUD

    http://www.cnblogs.com/cproom/archive/2006/11/13/559287.html NVPerfHUD是一个很好的3D程序调试工具,它是NVPerfKit的一部分 ...

  5. Shader Model 版本与DirectX的关系(OpenGL对应的呢?)

    http://blog.sina.com.cn/s/blog_6e521a600100q4dr.html DX9还是能支持到固定管线的,虽然说是在内部被转换成shader: DX10明确不再支持固定管 ...

  6. 使用VirtualEnvWrapper隔离python项目的库依赖

    是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装. 为什么要用 - 隔离项目之间的第三方包依赖,如 ...

  7. UITableview xib里面 cell 按钮的回调

    //  MoreBtnCell.m#import <UIKit/UIKit.h> @interface MoreBtnCell : UITableViewCell @property (w ...

  8. LR中获取当前系统时间方法

    方法一:使用loadrunner的参数化获取当前时间使用lr的参数化,非常方便,对lr熟悉的各位朋友也能马上上手,时间格式也有很多,可以自由选择.步骤:1.将复制给aa的值参数化2.选中abc,使用右 ...

  9. nginx反向代理初探

    1.安装nginx 2.在nginx.conf的http区段中配置负载均衡段 #cluserupstream myCluster{ server 192.168.1.110:1300 weight=5 ...

  10. 处理PHP字符串的10个简单方法;mysql出现乱码:character_set_server=utf8

    PHP处理字符串的能力非常强大,方法也是多种多样,但有的时候你需要选择一种最简单且理想的解决方法.文章列举了10个PHP中常见的字符串处理案例,并提供了相对应的最理想的处理方法. 1.确定一个字符串的 ...