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實(shí)現(xiàn)手勢(shì)滑動(dòng)解鎖功能簡(jiǎn)析
本篇文章主要介紹了iOS實(shí)現(xiàn)手勢(shì)滑動(dòng)解鎖功能簡(jiǎn)析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
iOS端React Native差異化增量更新的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS端React Native差異化增量更新的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
Reactnative-iOS回調(diào)Javascript的方法
這篇文章主要介紹了Reactnative-iOS回調(diào)Javascript的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法
這項(xiàng)目開(kāi)發(fā)中,有時(shí)候我們需要將本地的文件上傳到服務(wù)器,簡(jiǎn)單的幾張圖片還好,但是針對(duì)iPhone里面的視頻文件進(jìn)行上傳,為了用戶體驗(yàn),我們有必要實(shí)現(xiàn)斷點(diǎn)上傳。這篇文章主要介紹了iOS 斷點(diǎn)上傳文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-12-12
ios開(kāi)發(fā)中時(shí)間轉(zhuǎn)換的方法集錦
這篇文章主要介紹了ios開(kāi)發(fā)中時(shí)間轉(zhuǎn)換的方法集錦,需要的朋友可以參考下2015-05-05

