iOS UICollectionView实现横向滑动
更新时间:2020年6月30日 23:42 点击:1441
本文实例为大家分享了iOS UICollectionView实现横向滑动的具体代码,供大家参考,具体内容如下
UICollectionView的横向滚动,目前我使用在了显示输入框的输入历史上;
// // SCVisitorInputAccessoryView.m // 访客通行录入页面--访客姓名输入历史的InputAccessory #import "SCInputAccessoryView.h" #import "SCInputAccessoryCell.h" #define SCHorizontalMargin 15.0f #define SCVerticalMargin 10.0f @interface SCInputAccessoryView () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @property (weak, nonatomic) IBOutlet UICollectionView *collectionView; /// 名字记录的数组 @property (nonatomic, strong) NSMutableArray *nameArray; @end @implementation SCInputAccessoryView + (instancetype)loadNibView { return [[[NSBundle mainBundle] loadNibNamed:[SCInputAccessoryView className] owner:self options:nil] objectAtIndex:0]; } - (void)awakeFromNib { [super awakeFromNib]; self.clipsToBounds = YES; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self setupView]; } - (void)setupView { /// 设置此属性为yes 不满一屏幕 也能滚动 self.collectionView.alwaysBounceHorizontal = YES; self.collectionView.showsHorizontalScrollIndicator = NO; // 1.创建流水布局 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView.collectionViewLayout = layout; [self registerNibWithTableView]; } #pragma mark - 代理方法 Delegate Methods // 设置分区 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } // 每个分区上得元素个数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.nameArray.count; } // 设置cell - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SCInputAccessoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class]) forIndexPath:indexPath]; [cell refreshCellWithTitle:self.nameArray[indexPath.row]]; return cell; } // 设置cell大小 itemSize:可以给每一个cell指定不同的尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = 35.0f; CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height]; return CGSizeMake(width, height); } // 设置UIcollectionView整体的内边距(这样item不贴边显示) - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { // 上 左 下 右 return UIEdgeInsetsMake(SCVerticalMargin, SCHorizontalMargin, SCVerticalMargin, SCHorizontalMargin); } // 设置minimumLineSpacing:cell上下之间最小的距离 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return SCHorizontalMargin; } // 设置minimumInteritemSpacing:cell左右之间最小的距离 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return SCHorizontalMargin; } // 选中cell的回调 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.selectRowBlock) { self.selectRowBlock(indexPath.row, self.nameArray[indexPath.row]); } } #pragma mark - 对外方法 Public Methods /// array数组里面放的元素 必须字符串类型的 - (void)refreshUIWithNameArray:(NSArray<NSString *> *)array { [self.nameArray removeAllObjects]; [self.nameArray addObjectsFromArray:array]; [self.collectionView reloadData]; } #pragma mark - 内部方法 Private Methods // 注册cell - (void)registerNibWithTableView { [self.collectionView registerNib:[UINib nibWithNibName:[SCInputAccessoryCell className] bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SCInputAccessoryCell class])]; } - (CGFloat)gainStringWidthWithString:(NSString *)string font:(CGFloat)font height:(CGFloat)height { if (string.length == 0) { return 0.0f; } CGSize maxSize = CGSizeMake(MAXFLOAT, height); CGSize realSize = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size; /// 左右各16 return (realSize.width + 2 * (SCHorizontalMargin + 1.f)); } #pragma mark - 点击/触碰事件 Action Methods #pragma mark - 懒加载 Lazy Load - (NSMutableArray *)nameArray { if (!_nameArray) { _nameArray = [NSMutableArray arrayWithCapacity:0]; } return _nameArray; } @end
效果图:
Demo地址 :XGDevelopDemo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
iOS APP h5快捷程序 .mobileconfig的生成
1.从APP Store 下载Apple Configurator 2从一个管理点管理所有iOS设备应用程序,文档和配置文件。想要确保您的所有家庭成员在其每台iOS设备上都有类似的应用和文档,管理日益增...2021-12-23iOS设置UIButton文字显示位置和字体大小、颜色的方法
这篇文章给大家分享了iOS如何设置UIButton的文字显示位置和字体的大小、颜色,文中给出了示例代码,相信对大家的学习和理解很有帮助,有需要的朋友们下面来一起看看吧。...2020-06-30- 这篇文章主要为大家详细介绍了iOS如何将图片裁剪成圆形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-30
- 这篇文章主要给大家介绍了关于iOS给border设置渐变色的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-09
- 这篇文章主要为大家详细介绍了jQuery实现下拉菜单滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- 这篇文章主要介绍了iOS新版微信底部返回横条问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
- 这篇文章主要介绍了JavaScript 获取滚动条位置并将页面滑动到锚点的的相关资料,帮助大家更好的理解和学习使用JavaScript,感兴趣的朋友可以了解下...2021-02-09
- 这篇文章主要为大家详细介绍了uniapp 实现可以左右滑动导航栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-10-21
- 这篇文章主要介绍了vue+axios全局添加请求头和参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-24
- 这篇文章主要给大家介绍了关于iOS蓝牙设备名称缓存问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
- 这篇文章主要介绍了iOS新版微信底部工具栏遮挡问题完美解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-30
- 这篇文章主要介绍了封装 axios+promise通用请求函数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-12
- 这篇文章主要给大家介绍了关于iOS新增绘制圆的方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-30
- 这篇文章主要为大家详细介绍了iOS UIBezierPath实现饼状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-20
- 这篇文章主要为大家分享了js滑动提示效果代码,实现方法简单,感兴趣的小伙伴们可以参考一下...2016-03-12
- ios获取文件路径的方法,iphone沙箱模型的四个文件夹,通过documents,tmp,app,Library得到模拟器路径的简单方式,下面小编整理相关资料,把IOS获取各种文件目录路径的方式总结如下,需要的朋友可以参考下...2020-06-30
- 这篇文章主要介绍了Vue中 axios delete请求参数操作,具有很好的参考价值,希望对大家有所 帮助。一起跟随小编过来看看吧...2020-08-26
- vue项目经常会用到axios来请求数据,那么首先肯定需要对这个请求方法进行一个二次封装,这篇文章主要给大家介绍了关于项目中Axios二次封装的相关资料,需要的朋友可以参考下...2021-06-08
- 本文给大家介绍利用Vue实现tab导航栏,并且通过flex布局实现左右滑动效果,通过代码给大家分享tab导航栏布局的实现,本文给大家展示了完整代码,需要的朋友参考下吧...2021-06-28