时间:2021-05-19
首先来看看要实现的效果图
示例代码如下
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总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了jquery实现简单的二级导航下拉菜单效果。分享给大家供大家参考。具体如下:jQuery代码实现的二级导航菜单效果,非常简洁,喜欢简洁风格的朋友您
本文实例讲述了JS+CSS实现简单的二级下拉导航菜单效果。分享给大家供大家参考。具体如下:这是一款CSS配合JavaScript实现二级下拉导航菜单,好像CSS
本文实例为大家分享了vue.js实现二级菜单效果的具体代码,供大家参考,具体内容如下主要是对二级菜单和当前点击的处理:点击导航时,如果有二级菜单,就切换二级菜单
本文实例讲述了纯CSS实现超简单的二级下拉导航菜单代码。分享给大家供大家参考。具体如下:这是一款纯CSS菜单,二级下拉导航效果,是最简洁的CSS导航菜单,兼容性
本文实例讲述了js实现向右横向滑出的二级菜单效果。分享给大家供大家参考。具体如下:这是一个网页上的横向滑出二级菜单,菜单是竖向排列的,但二级子菜单项是向右横向滑