iOS实现简单的二级菜单效果
更新时间:2016年10月28日 08:55:03 作者:LYSNote
这篇文章给大家主要介绍的是利用iOS如何实现简单的菜单效果,文中给出了详细的示例代码,而且实现的比较简单,适合新人学习使用。感兴趣的朋友们可以参考借鉴,下面来一起看看吧。
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
首先来看看要实现的效果图
示例代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 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. } |
1 2 3 4 5 6 7 | #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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #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 |
总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
相关文章
iOS9 系统分享调用之UIActivityViewController
UIActivityViewController类是一个标准的view controller,通个使用这个controller,你的应用程序就可以提供各种服务。本文给大家介绍iOS9 系统分享调用之UIActivityViewController,感兴趣的朋友一起学习吧2015-11-11iOS中使用schema协议调用APP和使用iframe打开APP的例子
这篇文章主要介绍了iOS中使用schema协议调用APP和使用iframe打开APP的例子,用在浏览器中打开APP,需要的朋友可以参考下2014-08-08
最新评论