iOS實(shí)現(xiàn)簡(jiǎn)單的二級(jí)菜單效果
更新時(shí)間:2016年10月28日 08:55:03 作者:LYSNote
這篇文章給大家主要介紹的是利用iOS如何實(shí)現(xiàn)簡(jiǎn)單的菜單效果,文中給出了詳細(xì)的示例代碼,而且實(shí)現(xiàn)的比較簡(jiǎn)單,適合新人學(xué)習(xí)使用。感興趣的朋友們可以參考借鑒,下面來(lái)一起看看吧。
首先來(lái)看看要實(shí)現(xiàn)的效果圖

示例代碼如下
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
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問(wèn)大家可以留言交流。
您可能感興趣的文章:
- iOS 三級(jí)下拉菜單功能實(shí)現(xiàn)
- iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單
- iOS下拉選擇菜單簡(jiǎn)單封裝
- IOS中safari下的select下拉菜單文字過(guò)長(zhǎng)不換行的解決方法
- IOS代碼筆記之下拉菜單效果
- iOS中長(zhǎng)按調(diào)出菜單組件UIMenuController的使用實(shí)例
- iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項(xiàng)的編寫(xiě)方法講解
- iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類菜單
- 如何使用jQuery技術(shù)開(kāi)發(fā)ios風(fēng)格的頁(yè)面導(dǎo)航菜單
- iOS實(shí)現(xiàn)Pad上菜單彈出界面
相關(guān)文章
IOS如何使用CAShapeLayer實(shí)現(xiàn)復(fù)雜的View的遮罩效果
這篇文章主要為大家詳細(xì)介紹了IOS如何使用CAShapeLayer實(shí)現(xiàn)復(fù)雜的View的遮罩效果,感興趣的小伙伴們可以參考一下2016-03-03
iOS 中 使用UITextField格式化銀行卡號(hào)碼的解決方案
今天小編給大家分享ios中使用UITextField格式化銀行卡號(hào)碼的實(shí)現(xiàn)思路詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
iOS組件封裝與自動(dòng)布局自定義表情鍵盤(pán)
這篇文章主要介紹了iOS組件封裝與自動(dòng)布局自定義表情鍵盤(pán)的相關(guān)資料,需要的朋友可以參考下2016-04-04
ios動(dòng)態(tài)設(shè)置lbl文字標(biāo)簽的高度
本文給大家分享的是ios動(dòng)態(tài)設(shè)置lbl文字標(biāo)簽的高度寬度的方法,一共給大家匯總了3種方法,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇。2015-05-05

