iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >UICollectionView实现图片浏览效果
  • 143
分享到

UICollectionView实现图片浏览效果

2024-04-02 19:04:59 143人浏览 泡泡鱼
摘要

目录一、效果展示二、实现思路三、代码整理1、PhotoBrowseViewLayout2、PhotoBrowseCollectionViewCell3、CollectPhotoBro

一、效果展示

废话开篇:利用 UICollectionView 简单实现一个图片浏览效果。

二、实现思路

1.封装 UICollectionViewLayout ,实现内部 UICollectionViewCell 的布局。

UICollectionViewLayout 在封装瀑布流的时候会用到,而且担负着核心功能的实现。其实从另一个角度也可以把 UICollectionViewLayout 理解成“数据源”,这个数据不是 UI 的展示项,而是 UI 的尺寸项。在内部进行预计算 UICollectionViewCell 的 frame。

UICollectionView 是 UIScrollView的子类,只不过,它里面子控件通过“重用”机制实现了优化,一些复用的复杂逻辑还是扔给了系统处理。开发过程中只负责对 UICollectionViewLayout 什么时候需要干什么进行自定义即可。

2.获取 UICollectionView 目前可见的 cells,通过进行缩放、旋转变换实现一些简单的效果。

3、自定义 cell ,修改锚点属性。

三、代码整理

1、PhotoBrowseViewLayout

这里有一点需要注意的,在 UICollectionViewLayout 内部会进行计算每一个 cell 的 frame,在计算过程中,为了更好的展示旋转变换,cell 的锚点会修改到 (0.5,1),那么,为了保证 UI 展示不变,那么,就需要将 y 增加 cell 高度的一半。

#import "PhotoBrowseViewLayout.h"
@interface PhotoBrowseViewLayout()
@property(nonatomic,strong) NSMutableArray * attributeArray;
@property(nonatomic,assign) CGFloat cellWidth;
@property(nonatomic,assign) CGFloat cellHeight;
@property(nonatomic,assign) CGFloat sep;
@property(nonatomic,assign) int showCellNum;
@end
@implementation PhotoBrowseViewLayout
- (instancetype)init
{
    if (self = [super init]) {
        self.sep = 20;
        self.showCellNum = 2;
    }
    return self;
}
//计算cell的frame
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellWidth == 0) {
        self.cellWidth = **self**.collectionView.frame.size.width * 2 / 3.0;
    }
    if (self.cellHeight == 0) {
        self.cellHeight = self.collectionView.frame.size.height;
    }
    CGFloat x = (self.cellWidth + self.sep) * indexPath.item;
    //这里y值需要进行如此设置,以抵抗cell修改锚点导致的UI错乱
    CGFloat y = self.collectionView.frame.size.height / 2.0;
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attrs.frame = CGRectMake(x, y, self.cellWidth, self.cellHeight);
    return attrs;
}
//准备布局
- (void)prepareLayout
{
    [super prepareLayout];
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i <count; i++) {
        UICollectionViewLayoutAttributes *attris = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        [self.attributeArray addObject:attris];
    }
}
//返回全部cell的布局集合
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attributeArray;
}
//一次性提供UICollectionView 的 contentSize
- (CGSize)collectionViewContentSize
{
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    CGFloat maxWidth = count * self.cellWidth + (count - 1) * self.sep;
    return CGSizeMake(maxWidth, 0);
}
- (NSMutableArray *)attributeArray
{
    if (!_attributeArray) {
        _attributeArray = [[NSMutableArray alloc] init];
    }
    return _attributeArray;
}
@end

2、PhotoBrowseCollectionViewCell

这里主要是进行了锚点修改(0.5,1),代码很简单。

#import "PhotoBrowseCollectionViewCell.h"
@interface PhotoBrowseCollectionViewCell()
@property(nonatomic,strong) UIImageView * imageView;
@end
@implementation PhotoBrowseCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //设置(0.5,1)锚点,以底部中点为轴旋转
        self.layer.anchorPoint = CGPointMake(0.5, 1);
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 8;
    }
    return self;
}
- (void)setImage:(UIImage *)image
{
    self.imageView.image = image;
}
- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.contentView addSubview:_imageView];
    }
    return _imageView;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = **self**.contentView.bounds;
}
@end

3、CollectPhotoBrowseView

CollectPhotoBrowseView 负责进行一些 cell 的图形变换。

#import "CollectPhotoBrowseView.h"
#import "PhotoBrowseCollectionViewCell.h"
#import "PhotoBrowseViewLayout.h"
@interface CollectPhotoBrowseView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * photoCollectView;
@end
@implementation CollectPhotoBrowseView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self makeUI];
    }
    return self;
}
- (void)makeUI{
    //设置自定义 UICollectionViewLayout
    PhotoBrowseViewLayout * photoBrowseViewLayout = [[PhotoBrowseViewLayout alloc] init];
    self.photoCollectView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:photoBrowseViewLayout];
    self.photoCollectView.delegate = self;
    self.photoCollectView.dataSource = self;
    [self.photoCollectView reGISterClass:[PhotoBrowseCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    self.photoCollectView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.photoCollectView];
    //执行一次可见cell的图形变换
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self visibleCellTransfORM];
    });
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoBrowseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    [cell setImage: [UIImage imageNamed:[NSString stringWithFormat:@"fd%ld",indexPath.item % 3 + 1]]];
    return cell;
}
#pragma mark - 滚动进行图形变换
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //滑动的时候,动态进行cell图形变换
    [self visibleCellTransform];
}
#pragma mark - 图形变化
- (void)visibleCellTransform
{
    //获取当前可见cell的indexPath集合
    NSArray * visibleItems =  [self.photoCollectView indexPathsForVisibleItems];
    //遍历动态进行图形变换
    for (NSIndexPath * visibleIndexPath in visibleItems) {
        UICollectionViewCell * visibleCell = [self.photoCollectView cellForItemAtIndexPath:visibleIndexPath];
        [self transformRotateWithView:visibleCell];
    }
}
//进行图形转换
- (void)transformRotateWithView:(UICollectionViewCell *)cell
{
    //获取cell在当前视图的位置
    CGRect rect = [cell convertRect:cell.bounds toView:self];
    //计算当前cell中轴线与中轴线的距离的比值
    float present = ((CGRectGetMidX(rect) - self.center.x) / (self.frame.size.width / 2.0));
    //根据位置设置选择角度
    CGFloat radian = (M_PI_2 / 15) * present;
    //图形角度变换
    CGAffineTransform transformRotate = CGAffineTransformIdentity;
    transformRotate = CGAffineTransformRotate(transformRotate, radian);
    //图形缩放变换
    CGAffineTransform transformScale = CGAffineTransformIdentity
    transformScale = CGAffineTransformScale(transformScale,1 -  0.2 *  fabs(present),1 - 0.2 * fabsf(present));
    //合并变换
    cell.transform = CGAffineTransformConcat(transformRotate,transformScale);
}
@end

四、总结与思考

UICollectionView 也是 View,只不过系统为了更好的服务于开发者,快速高效的实现某些开发场景,进行了封装与优化,将复杂的逻辑单独的封装成一个管理类,这里就是 UICollectionViewLayout,交给它去做一些固定且复杂的逻辑。所以,自定义复杂UI的时候,就需要将功能模块足够细化,以实现更好的代码衔接。

以上就是UICollectionView 实现图片浏览效果的详细内容,更多关于UICollectionView 图片浏览的资料请关注编程网其它相关文章!

--结束END--

本文标题: UICollectionView实现图片浏览效果

本文链接: https://www.lsjlt.com/news/164748.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • UICollectionView实现图片浏览效果
    目录一、效果展示二、实现思路三、代码整理1、PhotoBrowseViewLayout2、PhotoBrowseCollectionViewCell3、CollectPhotoBro...
    99+
    2022-11-13
  • iOS UICollectionView实现卡片效果
    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo 实现上我选择了使用UICollectionView ;用UICollec...
    99+
    2022-05-28
    iOS 卡片
  • UIScrollView实现六棱柱图片浏览效果
    目录一、效果展示二、实现原理三、代码四、总结与思考一、效果展示 废话开篇:利用 CATransform3D 图形变换及 UIScrollView 的一些方法实现一个六棱柱图片浏览效果...
    99+
    2022-11-13
  • Android仿微信图片点击浏览的效果
    本篇我们来做一个类似于微信的图片点击浏览的效果,点击小图图片后会放大至全屏显示,且中间有一个2D平滑过渡的效果。 思路如下: 首先,从图片缩略界面跳转到图片详情页面,应...
    99+
    2022-06-06
    图片 Android
  • iOS使用UICollectionView实现横向滚动照片效果
    本文实例为大家分享了iOS使用UICollectionView实现横向滚动展示照片的具体代码,供大家参考,具体内容如下 这是Demo链接 效果图 思路 1. 界面搭建 界面的搭建十...
    99+
    2022-05-28
    iOS UICollectionView 横向滚动
  • 不同浏览器下图片滚动效果的js
    遇到过JS无法在火狐浏览器上实现IE的效果,昨天查看了一下相应的代码完成了这一功能的实现做一下简单的记录网上的资料很多但大多没有说明细节看到了峰之博文的一篇文章后才若有所思问题出现在那里。 <!-- end #piclist...
    99+
    2023-06-03
  • Android如何实现TV端大图浏览效果
    这篇文章主要介绍“Android如何实现TV端大图浏览效果”,在日常操作中,相信很多人在Android如何实现TV端大图浏览效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android如何实现TV端大图浏...
    99+
    2023-07-04
  • Vue+SSM实现图片上传预览效果
    现在的需求是:有一个上传文件按钮,当我们点击按钮时,可以选择需要上传的文件,确定后图片显示在界面上。 说明:本项目前端使用的Vue,后台用的SSM搭建的,服务器是Tomcat,数据库...
    99+
    2022-11-12
  • Android实现图片浏览器示例
    本文所述为一个基础的Android图片浏览器代码,是仿写Google原版实现的,代码中实现了主要的实现过程和方法,具体的完善还需要自己添加,代码中有很多注释,可帮助新手们快速理...
    99+
    2022-06-06
    浏览器 示例 图片 Android
  • Qt怎么实现图片浏览器
    这篇文章主要介绍了Qt怎么实现图片浏览器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Qt怎么实现图片浏览器文章都会有所收获,下面我们一起来看看吧。图片浏览器逻辑实现图片浏览器用到了前面几乎所有的知识,包括窗口...
    99+
    2023-07-05
  • Qt实战之实现图片浏览器
    目录图片浏览器逻辑效果图具体实现utils.himage.himage_group.himage_group.cppqimgviewwidget.hqimgviewwidget.cp...
    99+
    2023-03-19
    QT实现图片浏览器 QT图片浏览器 QT图片
  • Android编程实现图片的浏览、缩放、拖动和自动居中效果
    本文实例讲述了Android编程实现图片的浏览、缩放、拖动和自动居中效果的方法。分享给大家供大家参考,具体如下: Touch.java public class Touch...
    99+
    2022-06-06
    自动 居中 图片 Android
  • iOS开发UICollectionView实现拖拽效果
    一.介绍 iOS9提供API实现单元格排序功能,使用UICollectionView及其代理方法。iOS9之后有自带方法可以实现该效果,只需添加长按手势,实现手势方法和调用iOS9的...
    99+
    2022-05-23
    iOS UICollectionView 拖拽
  • Android仿淘宝商品浏览界面图片滚动效果
    用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个ScrollView滚动到最底下时会有提示,继续拖动才能浏览图片。仿照这个效果写一个出来并不难,只要定义一个Layout...
    99+
    2022-06-06
    淘宝 界面 图片 淘宝商品 动效 Android
  • Android实现TV端大图浏览效果的全过程
    目录前言1.BitmapRegionDecoder:2.SubsamplingScaleImageView3.Glide+SubsamplingScaleImageView混合加载渲...
    99+
    2023-01-06
    android TV开发 android点击查看大图 android超大图加载
  • iOS开发实现图片浏览功能
    本文实例为大家分享了iOS实现图片浏览功能的具体代码,供大家参考,具体内容如下 这是整体的效果图: 其中main.stroyboard中的控件有2个button,2个label,一...
    99+
    2022-11-13
  • Android简易图片浏览器的实现
    闲着没事,花了半个小时用这几天发布的部分内容来做一个Android简易图片浏览器小程序,代码设main_activity.xml设计首界面,设计三个按钮并赋予id,然后在MainAc...
    99+
    2022-11-13
  • 基于Qt实现SVG图片浏览器
    目录介绍一、项目介绍二、项目基本配置三、UI界面设计四、主程序实现4.1 .pro文件4.2 添加SvgWindow类4.3 添加SvgWidget类4.4 svgwidget.h头...
    99+
    2022-11-13
  • Android实现网络图片浏览功能
    我们在上网的过程中经常看到各种图片,那你知道它是如何实现的吗?接下来就让我们一块探讨一下。 网络图片的浏览可以分为俩部分,基本的页面布局与界面交互,让我们一步步的来编写。 基本布局很简单,只需要有一个输入图片链接的EditText,一个浏览...
    99+
    2023-05-31
    android 图片浏览 roi
  • QT实战之实现图片浏览系统
    目录引言实现功能效果实现图片浏览所用知识实现流程实现环境和UI设计具体实现引言 本系统支持,自动播放,左右拖动切换,点击列表切换,点击按钮切换;是一个标准的图像浏览软件。 Windo...
    99+
    2023-05-17
    QT实现图片浏览系统 QT图片浏览系统 QT图片浏览
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作