国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS實(shí)現(xiàn)簡單的二級菜單效果

2019-10-21 18:51:12
字體:
供稿:網(wǎng)友

首先來看看要實(shí)現(xiàn)的效果圖

ios實(shí)現(xiàn)二級菜單欄,ios下拉菜單實(shí)現(xiàn),ios下拉菜單的實(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;@endstatic 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é)

以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 格尔木市| 潼关县| 雅江县| 聂拉木县| 肃北| 彭泽县| 锡林郭勒盟| 宜春市| 阳城县| 竹溪县| 西乌| 高雄县| 塔河县| 牡丹江市| 瑞昌市| 玛纳斯县| 澎湖县| 桐庐县| 汉沽区| 白银市| 易门县| 都昌县| 广南县| 临桂县| 金寨县| 香河县| 咸宁市| 黑水县| 阳原县| 乡宁县| 宜兴市| 龙海市| 宿州市| 友谊县| 靖江市| 天全县| 桑植县| 江源县| 越西县| 寿阳县| 深州市|