iOS实现简单的二级菜单效果

首先来看看要实现的效果图

示例代码如下

Untitled.gif
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionSectionView.h"
@interface ViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)NSString *leftOrRight;
@property (nonatomic,strong)NSIndexPath *clickIndexPath;
@end
static NSString *cellIdentifier = @"CollectionViewCell";
static NSString *sectionIdentifier = @"CollectionSectionView";
@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
 flowLayout.itemSize = CGSizeMake(50, 25);
 flowLayout.minimumLineSpacing = 0;
 flowLayout.minimumInteritemSpacing = 0;
 flowLayout.headerReferenceSize = CGSizeMake(0, 40);
 flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
 self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
 _collectionView.backgroundColor = [UIColor whiteColor];
 _collectionView.delegate = self;
 _collectionView.dataSource = self;
 [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
 [_collectionView registerClass:[CollectionSectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionIdentifier];
 [self.view addSubview:_collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
 return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 if (self.clickIndexPath.section == section) {
  if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"left"]) {
   return 3;
  }
  if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"right"]) {
   return 4;
  }
  if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"left"]) {
   return 10;
  }
  if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"right"]) {
   return 8;
  }
  if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"left"]) {
   return 33;
  }
  if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"right"]) {
   return 6;
  }
 }
 return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
 cell.label.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
 return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  CollectionSectionView *sectionView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:sectionIdentifier forIndexPath:indexPath];
  sectionView.backgroundColor = [UIColor lightGrayColor];
  sectionView.leftStr = @"家具";
  sectionView.rightStr = @"家电";
  [sectionView customBtnHandelAction:^(NSString *leftOrRight) {
   self.clickIndexPath = indexPath;
   self.leftOrRight = leftOrRight;
   [collectionView reloadData];
  }];
  return sectionView;
 }
 return nil;
}
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
#import <UIKit/UIKit.h>
typedef void(^BtnHandleAction)(NSString *leftOrRight);
@interface CollectionSectionView : UICollectionReusableView
@property (nonatomic,copy)NSString *leftStr;
@property (nonatomic,copy)NSString *rightStr;
- (void)customBtnHandelAction:(BtnHandleAction)action;
@end
#import "CollectionSectionView.h"
@interface CollectionSectionView ()
@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;
@property (nonatomic,copy)BtnHandleAction btnHandelAction;
@end
@implementation CollectionSectionView
- (instancetype)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
 if (self) {
  [self setup];
 }
 return self;
}
- (UIButton *)leftBtn{
 if (!_leftBtn) {
  self.leftBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  _leftBtn.tag = 2000;
  _leftBtn.frame = CGRectMake(1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
  _leftBtn.backgroundColor = [UIColor whiteColor];
  [_leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
 }
 return _leftBtn;
}
- (UIButton *)rightBtn{
 if (!_rightBtn) {
  self.rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  _rightBtn.tag = 2001;
  _rightBtn.frame = CGRectMake(self.frame.size.width / 2 + 1, 1, self.frame.size.width / 2 - 2, self.frame.size.height - 2);
  _rightBtn.backgroundColor = [UIColor whiteColor];
  [_rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
 }
 return _rightBtn;
}
- (void)btnAction:(UIButton *)sender{
 NSInteger tag = sender.tag;
 if (tag == 2000) {
  self.btnHandelAction(@"left");
 }
 if (tag == 2001) {
  self.btnHandelAction(@"right");
 }
}
- (void)customBtnHandelAction:(BtnHandleAction)action{
 self.btnHandelAction = action;
}
- (void)setup{
 [self addSubview:self.leftBtn];
 [self addSubview:self.rightBtn];
}
- (void)setLeftStr:(NSString *)leftStr{
 [self.leftBtn setTitle:leftStr forState:(UIControlStateNormal)];
 [self.leftBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
- (void)setRightStr:(NSString *)rightStr{
 [self.rightBtn setTitle:rightStr forState:(UIControlStateNormal)];
 [self.rightBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
}
@end

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。

(0)

相关推荐

  • iOS10 widget实现3Dtouch 弹出菜单

    文章将依次从以下几个问题着手,进行详细说明: 1.如何为现有的工程添加widget: 2.如何绘制UI: 3.如何调起app: 4.如何与host app共享数据. 图2 添加today的target 图3 添加today之后的工程目录 这是添加Today Extension之后的工程目录. 到这里,为现有的工程添加Today Extension算是完成了,运行程序就可以看到类似图1的简单的效果了,很简单哈. 绘制UI 图4 删除默认创建的MainInterface并修改Info.plist 这

  • iOS下拉选择菜单简单封装

    本文实例为大家分享了简单封装的iOS下拉选择菜单代码,供大家参考,具体内容如下 // // OrderListDownMenu.h #import <UIKit/UIKit.h> @protocol OrderListDownMenuDelegate <NSObject> - (void)OrderListDownMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; @end

  • iOS从App跳转至系统设置菜单各功能项的编写方法讲解

    跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"]; if ([[UIApplication sharedApplication] canOpenURL:url]) {     [[UIApplication sharedApplicatio

  • iOS实现Pad上菜单弹出界面

    前言: 此种方式实现只适用于pad开发,在iPhone上是无效的. 实现: 比如我在界面上有一个按钮,点击按钮,在按钮旁边弹出一个Pop框. 1.按钮点击事件 btn.addTarget(self, action: #selector(self.popShow), for: .touchUpInside) 2.事件处理 /// 弹框选择条件 /// /// - Parameter sender: <#sender description#> func popShow(sender:UIButt

  • IOS代码笔记之下拉菜单效果

    本文实例为大家分享了ios下拉菜单的具体代码,供大家参考,具体内容如下 一.效果图 二.工程图 三.代码 RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end RootViewController.m #import "RootViewController.h" #import "NIDropDown.h" @i

  • IOS中safari下的select下拉菜单文字过长不换行的解决方法

    今天遇到下图这种问题,文字过长,显示不全.折腾了老半天,在网上搜了半天也找不到解决方案. 于是问了下同事,同事提到了<optgroup>,这个标签厉害. <optgroup> 标签定义选项组. optgroup 元素用于组合选项.当您使用一个长的选项列表时,对相关的选项进行组合会使处理更加容易. 以上所述是小编给大家介绍的IOS中safari下的select下拉菜单文字过长不换行的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的.在此也非常感谢大家对

  • iOS 三级下拉菜单功能实现

    前言 App 常用控件 -- 多级下拉菜单, 如团购类, 房屋类, 对数据进行筛选. 有一级, 二级, 三级, 再多就不会以这种样式,呈现给用户了. 作者就简单聊一下 多级下拉菜单 一 目标 默认显示一个 TableView, 点击数据后, 添加第二个TableView, 并实现大小变化 第二次打开下拉菜单. 保存上次选中数据 二 菜单控件DropMenuView .h文件 #import <UIKit/UIKit.h> @class DropMenuView; @protocol DropM

  • 如何使用jQuery技术开发ios风格的页面导航菜单

    效果图: 目前市场上越来越流行IOS风格的操作系统和导航方式,在今天的jQuery教程中,我们介绍如何生成一个iphone风格的菜单导航. HTML代码 我们使用镶嵌的<li>来生成菜单内容,并且包含在<nav>标签中,如下: <nav> <h1>导航菜单</h1> <ul> <li> <h2>专题教程</h2> <ul> <li> <h3>HTML专题教程<

  • iOS实现顶部标签式导航栏及下拉分类菜单

    本文实例为大家分享了iOS实现顶部标签式导航栏及下拉分类菜单的全部过程,供大家参考,具体内容如下 当内容及分类较多时,往往采用顶部标签式导航栏,例如网易新闻客户端的顶部分类导航,最近刚好有这样的应用场景,参考网络上一些demo,实现了这种导航效果,记录一些要点. 效果图(由于视频转GIF掉帧,滑动和下拉动画显得比较生硬,刚发现quickTime可以直接录制手机视频,推荐一下,很方便) 1.顶部标签式导航栏 (1)实现思路 其实就是在上下两个UIScrollView上做文章,实现联动选择切换的效果

  • iOS中长按调出菜单组件UIMenuController的使用实例

    UIMenuController的使用 UIMenuController的展现需要基于一个View视图,其交互则需要基于其所在View视图的Responder.举例来说,如果一个UIMenuController展现在当前ViewController的View上,则此UIMenuController的交互逻辑交由当前的ViewController进行管理. 在界面展示出UIMenuController需要3个条件: 1.当前的Responder处于第一响应. 2.UIMenuController对

随机推荐