IOS开发之瀑布流的实现

上传人:cn****1 文档编号:494545432 上传时间:2023-11-24 格式:DOC 页数:12 大小:283.50KB
返回 下载 相关 举报
IOS开发之瀑布流的实现_第1页
第1页 / 共12页
IOS开发之瀑布流的实现_第2页
第2页 / 共12页
IOS开发之瀑布流的实现_第3页
第3页 / 共12页
IOS开发之瀑布流的实现_第4页
第4页 / 共12页
IOS开发之瀑布流的实现_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《IOS开发之瀑布流的实现》由会员分享,可在线阅读,更多相关《IOS开发之瀑布流的实现(12页珍藏版)》请在金锄头文库上搜索。

1、if (group) groupenumerateAssetsUsingBlock:a( ALAsset *result, NSUlnteger index, BOOLBOOLstop) if(result) ALAssetRepresentation*resentation = resultdefaultReprlibraryenumerateGroupsWithTypes :ALAssetsGroupSavedPhotos usingBlock:A(ALAssetsGroup *group, BOOLBOOLstop) esentation ;CGlmageRef imageRef = r

2、esentation.fullResolutionlmage;Ullmage *image = Ullmage imageWithCGlmage :imageRef;【无限互联】IOS开发之瀑布流的实现(UlCollectionView 与UlScrollView)瀑布流可以在保证图片原始比例的情况下,灵活的展现内容,相对于传统的使用相同大小的网格展现大量图片,要好上很多,而实现瀑布流的方式有很多种,网上比较流行的有三种实 现方式。1,使用UlScrollView,主要技术点在于视图的重用。2,使用UlTableView,这种方式应该是最易想到的,因为需要展现几列就用几个tabelview就o

3、k 了,而且不需要考虑重用,应为苹果已经做好了,只需要考虑如何在几列tabelView滑动的时候,保持同步不出现 BUG。3,使用 UlCollectionView , UlCollectionView在 iOS6 中第一次被介绍,它与 UlTableView有许多相似点,但它多了一个布局类,而实现瀑布流,就与这个布局类有关。此种方式实现, 也不需要考虑视图重用。以上三种方式实现瀑布流,使用UlCollectionView应该算是最简单的了 ,so,就重最简单的开始吧。由于网络太差,所以展现的并不是网络上的图片,而是将用户相册中的图片读取出,并用瀑布流展现。首先,遍历用户相册,将照片放到准备好

4、的数组中。访问用户相册需要导入框架objc view pla in copy1.2.3.4.5.6.7.8.9.10.11.12. _images = NSMutableArrayarray ;/创建相册库library = ALAssetsLibraryalloc init ;13.14.15.16.17.18.19.20.21.22.23.24./将相片加入到数组中self .images addObject :image;_collectionViewreloadData ;; failureBlock:A( NSError *error) ;v/span然后创建UlCollection

5、View ,注意这里放置 UlCollectionView 的控制器要实现,UICollectionViewDelegateFlowLayout和 UlCollectionViewDatasource 两个协议。UICollecti on View创建时需要传入一个布局对象,布局类继承自UlCollectionViewLayout这个抽象基类,我们需要自定义布局对象,继承UlCollectio nV iewLayout的子类,UlCollectio nViewFlowLayout类,命名为WaterFlowLayout 。objc view pla in copy1.vspan style=f

6、ont-size:14px;interface WaterFlowLayout : UlCollectionViewFlowLayout2.property (nonatomic ,assign)id delegate;3.4.end5.1objcview pla in copy1.vspan style=font-size:14px;WaterFlowLayout *layout = WaterFlowLayout alloc init ;2.3._collectionView = UlCollectionViewalloc initWithFrame:UlScreenmainScreen

7、.bounds collectionViewLayout :layout;5.6.7.8.9.10._collectionView.dataSource =self ;_collectionView.delegate =self ; self .view addSubview :_collectionView;/注册单元格_collectionViewregisterClass:WaterfallCellclass forCellWithReuseldentifier:indf;11.v/span设置好代理,下面实现代理协议就行了,在这里同样子类化了UlCollectio nV iewCell

8、,命名为WaterfallCell,并设置一个image属性,便于在协议方法中将照片传入。objcview pla in copy1.vspan style= font-size:14px;interface WaterfallCell : UlCollectionViewCel2.3.property ( nonatomic , retain ) Ullmage *image; enc实现 UlCollectionViewDatasource协议。objcview pla in copy1.vspan style= font-size:14px;- (NSlnteger)collection

9、View:(UlCollectionView*)collectionView numberOfltemsInSection :(NSInteger)section2.3.4.5.return _images.count;/ The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseldentifier:forlndexPath:6.-(UlCollectionViewCell*)collectionView:(UlCollectionView*)collectionViewce

10、llForltemAtlndexPath :( NSIndexPath *)indexPath7.WaterfallCell*cell = collectionViewdequeueReusableCellWithReuseldentifier:indf forlndexPath:indexPath;8.9.10.11.12.13.14.Ullmage *image = _imagesindexPath.item; cell.image = image;return cell;实现 UlCollectionViewDelegateFlowLayout协议,将单元格的尺寸传入, 这里让每张图片的

11、宽度保持相同,高度等比例缩放。objc view pla in copy1.-(CGSize)collectionView:(ollectionView layout :(UICollectionViewLayout*)collectionViewLayoutUICollectionView *)csizeForltemAtlnd2.3.exPath :( NSIndexPath *)indexPath UIImage *image = _imagesindexPath.item;4.5.6.float height = self imgHeight:image.size.heightWidt

12、h :image.size.width7.8.return CGSizeMake(100,height);9.10.11./单元格中的间隙12.-(UIEdgeInsets)collectionView:(UICollectionView*)collectionViewlayoutCollectionViewLayout*)collectionViewLayoutinsetForSectionAtIndex:(NSInteger)section13.UIEdgeInsets edgeInsets = 5,5,5,5;14.15.return edgeInsets;16.17.-(float )

13、imgHeight:( float )heightWidth :( float )width18.19./单元格固定宽度为100高度/宽度=压缩后高/10020.float newHeigth = height/width*100;21.return newHeigth;22.23.v/span:(UI以上完成以后,控制器中的逻辑基本处理完毕,下面重写cell的setter方法,简单起见,我并没有创建Imageview显示图片,而是将图片直接绘制在了单元格上。| objcview pla in copy1. - ( void )setImage:( UIImage *)image2. if (_image != image) 3. m age = image;4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.I1.2.3.4.5.6.7.8.9.10.11.1

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号