ViewController 调用

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

self.title = @"测试";

self.view.backgroundColor = [UIColor whiteColor];

//自定义调用

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"1",@"2",@"3",@"4",@"5",@"6",@"1",@"2", nil];

NSMutableArray *textArr = [[NSMutableArray alloc] initWithObjects:@"全部",@"女装",@"箱包",@"配饰",@"内衣",@"男装",@"美妆",@"母婴",@"居家",@"家电",@"食品",@"鞋靴",@"全部",@"女装", nil];

//    self.customView = [[CustomView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100) dataArray:arr];

self.customView = [[CustomView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100) dataArray:arr textArray:textArr];

[self.view addSubview:self.customView];

}

//一级页面视图  CustomView

#import <UIKit/UIKit.h>

#import "CustomFenLeiChildView.h"

//自定义头文件

@interface CustomView : UIView

{

//记录相同按钮的Y

float ButtonY;

//记录选中的分类子视图tag

int selectedTag;

CustomFenLeiChildView *lastCustomView;

CustomFenLeiChildView *customView;

//记录button

UIButton *laseButton;

//是否是展开

BOOL isZhanKai;

}

@property (nonatomic, strong)NSMutableArray *dataArr;

-(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray;

-(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray textArray:(NSMutableArray *)textArray;

@end

//一级视图实现文件.m 

#import "CustomView.h"

//自定义实现文件

@implementation CustomView

-(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray

{

self = [super initWithFrame:frame];

if (self)

{

//button坐标

float bx = 0;

float by = 0;

float bw = frame.size.width/4;

float bh = bw;

//imageView坐标

float imgX = 10;

float imgW = bw-20;

float imgH = imgW;

float imgY = 10;

for (int i = 0; i < dataArray.count; i++)

{

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//            button.backgroundColor = [UIColor yellowColor];

if (i % 4 == 0 && i > 0)

{

bx = 0;

by += bh;

}

button.frame = CGRectMake(bx, by, bw, bh);

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];

//            imgView.backgroundColor = [UIColor redColor];

imgView.image = [UIImage imageNamed:@"1.png"];

[button addSubview:imgView];

[self addSubview:button];

bx += bw;

}

}

return self;

}

-(id)initWithFrame:(CGRect)frame dataArray:(NSMutableArray *)dataArray textArray:(NSMutableArray *)textArray

{

self = [super initWithFrame:frame];

if (self)

{

self.dataArr = [[NSMutableArray alloc] init];

self.dataArr = textArray;

//button坐标

float bx = 0;

float by = 0;

float bw = frame.size.width/4;

float bh = bw;

//imageView坐标

float imgX = 15;

float imgW = bw-30;

float imgH = imgW;

float imgY = 15;

float selfHeight = 0.0;

for (int i = 0; i < dataArray.count; i++)

{

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.tag = i+100;

if (i % 4 == 0 && i > 0)

{

bx = 0;

by += bh;

ButtonY = by;

}

button.frame = CGRectMake(bx, by, bw, bh);

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];

imgView.layer.cornerRadius = imgW/2;

imgView.layer.masksToBounds = YES;

imgView.image = [UIImage imageNamed:@"1.png"];

[button addSubview:imgView];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imgX, imgY+imgH+3, imgW, 20)];

label.text = [textArray objectAtIndex:i];

label.font = [UIFont systemFontOfSize:14.0];

label.textColor = [UIColor colorWithRed:65/255.0 green:65/255.0 blue:65/255.0 alpha:1];

label.textAlignment = NSTextAlignmentCenter;

[button addSubview:label];

bx += bw;

selfHeight = by+bh;

}

CGRect selfFrame = self.frame;

selfFrame.size.height = selfHeight;

self.frame = selfFrame;

}

return self;

}

-(void)buttonClick:(UIButton *)btn

{

BOOL bl = NO;

int index = (int)btn.tag - 100;

if (laseButton)

{

ButtonY = laseButton.frame.origin.y;

}

if(btn && btn.tag == laseButton.tag)

{

bl = YES;

}

if (lastCustomView)

{

[lastCustomView removeFromSuperview];

lastCustomView = nil;

}

CGRect btnFrame = btn.frame;

if (!bl)

{

laseButton = btn;

isZhanKai = YES;

UILabel *label = [btn.subviews lastObject];

NSLog(@"%@",label.text);

float childViewY = btnFrame.origin.y+btnFrame.size.height + 10;

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"上衣",@"裤装",@"裙装",@"套装",@"大码", nil];

CustomFenLeiChildView *currntView = [[CustomFenLeiChildView alloc] initWithFrame:CGRectMake(0, childViewY, self.frame.size.width, 120) fenLeiArray:array];

[self addSubview:currntView];

selectedTag = index+1000;

currntView.tag = selectedTag;

lastCustomView = currntView;

customView = currntView;

[self updateUI:btnFrame childViewFrame:currntView.frame index:index];

}

else

{

isZhanKai = NO;

laseButton = nil;

UILabel *label = [btn.subviews lastObject];

NSLog(@"%@",label.text);

customView = (CustomFenLeiChildView *)[self viewWithTag:selectedTag];

CGRect customRect = customView.frame;

customRect.size.height = 0;

[customView removeFromSuperview];

[self updateUI:btnFrame childViewFrame:customRect index:index];

}

}

-(void)updateUI:(CGRect)btnFrame childViewFrame:(CGRect)childViewFrame index:(int)index

{

//去掉原来的展开视图

//重新排列button

//获取当前点击buton的y

//设置展开视图

//后面的修改view

//判断:1.当前展开视图是否展开(如果是展开,获取到当前点击的button,计算展开在哪,如果不需要展开,获取到当前点击的button,计算之后的button的坐标)

//--------------重新排列

//button坐标

float bx = 0;

float by = 0;

float bw = self.frame.size.width/4;

float bh = bw;

float selfHeight = 0.0;

for (int i = 0; i < self.dataArr.count; i++)

{

UIButton *button = (UIButton *)[self viewWithTag:i+100];

CGRect buttonFrame = button.frame;

if (i % 4 == 0 && i > 0)

{

bx = 0;

by += bh;

}

buttonFrame = CGRectMake(bx, by, bw, bh);

button.frame = buttonFrame;

bx += bw;

selfHeight = by+bh;

}

if (isZhanKai)

{

bx = 0;

if (btnFrame.origin.y > ButtonY)

{

UIButton *button = (UIButton *)[self viewWithTag:index+100];

customView = (CustomFenLeiChildView *)[self viewWithTag:index+1000];

CGRect customRect = customView.frame;

customRect.origin.y = button.frame.origin.y+button.frame.size.height+10;

customView.frame = customRect;

by = customView.frame.origin.y+customView.frame.size.height;

}

else

{

by = childViewFrame.origin.y+childViewFrame.size.height;

}

//获取当前的索引

int a = index % 4;

int c = 4 - a;

//获取要开始计算的button的索引

int b = index + c;

if (b > self.dataArr.count)

{

b = self.dataArr.count;

}

for (int i = b; i < self.dataArr.count; i++)

{

UIButton *button = (UIButton *)[self viewWithTag:i+100];

CGRect buttonFrame = button.frame;

if (i % 4 == 0 && i > b)

{

bx = 0;

by += bh;

}

buttonFrame = CGRectMake(bx, by, bw, bh);

button.frame = buttonFrame;

bx += bw;

selfHeight = by+bh;

}

}

else

{

NSLog(@"不是展开");

}

CGRect selfFrame = self.frame;

selfFrame.size.height = selfHeight;

self.frame = selfFrame;

}

 //展开详情(二级页面视图) CustomFenLeiChildView

#import <UIKit/UIKit.h>

#import "LabelOfButton.h"

@interface CustomFenLeiChildView : UIView

-(id)initWithFrame:(CGRect)frame fenLeiArray:(NSMutableArray *)fenLeiArray;

@end

//展开详情实现文件

#import "CustomFenLeiChildView.h"

#define lineBgColor [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1]

@implementation CustomFenLeiChildView

-(id)initWithFrame:(CGRect)frame fenLeiArray:(NSMutableArray *)fenLeiArray

{

self = [super initWithFrame:frame];

if (self)

{

UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];

topLine.backgroundColor = lineBgColor;

[self addSubview:topLine];

//button坐标

float bx = 0;

float by = 0;

float bw = (frame.size.width-1)/2;

float hang = fenLeiArray.count/2;

float bh = frame.size.height/hang-1*(hang-1);

float selfHeight = 0.0;

for (int i = 0; i < fenLeiArray.count; i++)

{

if (i % 2 == 0 && i > 0)

{

bx = 0;

by += bh;

}

LabelOfButton *button = [[LabelOfButton alloc] initWithFrame:CGRectMake(bx, by, bw, bh)];

button.tag = i+100;

button.textLB.text = fenLeiArray[i];

button.textLB.textAlignment = NSTextAlignmentCenter;

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(bx, by+bh, bw, 1)];

bottomLine.backgroundColor = lineBgColor;

[self addSubview:bottomLine];

UIView *shuLine = [[UIView alloc] initWithFrame:CGRectMake(bx+bw, by, 1, bh)];

shuLine.backgroundColor = lineBgColor;

[self addSubview:shuLine];

bx += bw;

selfHeight = by+bh;

}

if (fenLeiArray.count % 2 == 1)

{

LabelOfButton *button = [[LabelOfButton alloc] initWithFrame:CGRectMake(bx, by, bw, bh)];

button.textLB.text = @"最后一个";

button.tag = fenLeiArray.count+100;

//            button.textLabel.text = fenLeiArray[i];

button.textLB.textAlignment = NSTextAlignmentCenter;

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:button];

UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(bx, by+bh, bw, 1)];

bottomLine.backgroundColor = lineBgColor;

[self addSubview:bottomLine];

}

CGRect selfFrame = self.frame;

selfFrame.size.height = selfHeight;

self.frame = selfFrame;

}

return self;

}

-(void)buttonClick:(LabelOfButton *)btn

{

NSLog(@"%@",btn.textLB.text);

}

//自定义button ,添加label

#import <UIKit/UIKit.h>

//#import "NewCustomLabel.h"

@interface LabelOfButton : UIButton

@property (nonatomic, strong) UILabel *textLB;

@end

#import "LabelOfButton.h"

#define GrayColor [UIColor colorWithRed:165.0/255.0 green:165.0/255.0 blue:165.0/255.0 alpha:1.0]

@implementation LabelOfButton

-(id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.textLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

[self.textLB setFont:[UIFont systemFontOfSize:16]];

self.textLB.textColor = GrayColor;

self.textLB.textAlignment = NSTextAlignmentLeft;

[self addSubview:self.textLB];

}

return self;

}

@end

实现效果如下图所示

              

iOS 展示二级页面的更多相关文章

  1. vue爬坑之路1-路由跳转全新页面以及二级页面配置

    之前也在网找了一些答案,比较零碎,特此总结下,废话不多说,直接上干货! 路由跳转全新页面 首先在app.vue中先定义router-view,这是主路由. 在router的index.js中引入log ...

  2. IOS的H5页面滑动不流畅的问题:

    IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK

  3. Html5 Egret游戏开发 成语大挑战(八)一般性二级页面处理

    在游戏中,我们一般会有各种各样的二级页面,比如游戏暂停界面或者游戏结束界面,这些界面组成了对玩家交互主要手段,在游戏开发中,对于这些界面的coding组织是非常有学问的,如果倒退到十年前,游戏开发的老 ...

  4. 关于html5与jsp页面同样的html代码展示的页面效果不一样的问题

    原文:关于html5与jsp页面同样的html代码展示的页面效果不一样的问题 html5默认的声明为 <!DOCTYPE html> jsp默认的声明头部为 <%@ page con ...

  5. 用github展示前端页面

    今天尝试了一下使用github展示前端页面,还是比较简单的,平时做一些小的demo时,可以用这个方法展示自己的页面,在此记录下方法. 首先打开自己的github项目仓库,比如   https://gi ...

  6. web scraper 抓取分页数据和二级页面内容

    如果是刚接触 web scraper 的,可以看第一篇文章. web scraper 是一款免费的,适用于普通用户(不需要专业 IT 技术的)的爬虫工具,可以方便的通过鼠标和简单配置获取你所想要数据. ...

  7. ios访问web页面<div>点击事件不起效果,以及alert()显示url的解决办法

    ios访问web页面<div>点击不起效果,在其div上添加style=”cursor:pointer:“ jquery web页面动态append()事件调用方法:$(document) ...

  8. Android ios嵌套web页面

    我们现在做一个活动页面,Android和ios的活动页面用web来做,方便更改,下面有几个小问题: 1.在Android和ios中,虽然web上面可以存localstorage,但是到了Android ...

  9. ios嵌套H5页面,出现的小bug;

    ios嵌套H5页面,点击数字时就会弹出打电话的功能:解决方法: 在head标签中添加: <meta name="format-detection" content=" ...

随机推荐

  1. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  2. [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据

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

  3. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. hdu 4283 You Are the One 区间dp

    You Are the One Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. (二)Kafka动态增加Topic的副本(Replication)

    (二)Kafka动态增加Topic的副本(Replication) 1. 查看topic的原来的副本分布 [hadoop@sdf-nimbus-perf ~]$ le-kafka-topics.sh ...

  6. 安装openJDK 8

    1.JDK 8 示例 (1.1)Debian, Ubuntu等使用下述安装命令: $ -jre ps : openjdk-8-jre 仅包含JRE,如果需要开发java程序,需要下载openjdk-8 ...

  7. 单据类型BE构建

    开发流程:BE--UI--发布在开发时有必要对元数据.代理调用.弱类型.强类型有所了解基本的单据开发包括单据类型BE.单据状态BE.单据BE,下面分别罗列出各单据的BE的设计开发: 本章主要讲解单据类 ...

  8. STM32学习笔记(八) SPI总线(操作外部flash)

    1. SPI总线简介 SPI全称串行外设接口,是一种高速,全双工,同步的外设总线:它工作在主从方式,常规需要至少4根线才能够正常工作.SPI作为基本的外设接口,在FLASH,EPPROM和一些数字通讯 ...

  9. Android 控件知识点,

    一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...

  10. Karma Police - Radiohead

    音乐赏析似乎是一件没有意义的工作,与电影相比音乐更加抽象,不同的人对同一首歌会有完全不同的解读. 但一首歌一旦成为经典,就有解读它的必要,因为它一定诉出了一个群体的某些情绪. Karma police ...