给我两分钟的时间:微博风格九宫格:UICollectionView实现

3 篇文章 0 订阅

引言

UICollectionView 是 iOS 平台上一种强大的视图布局工具,能够很好地实现网格布局,列表布局等多种布局方式。

首先讲下今天的目标,我们将要使用 UICollectionView 来创建仿微博的九宫格内容。首先,目标行数为3,每行显示3张图片,总共显示9张图片。

实现方式

我们往界面上添加一个 UICollectionView,并创建一个 UICollectionViewFlowLayout 布局类,这里的实现为懒加载的方式:

- (UICollectionView *)collectionView {
    if (!_collectionView) {
	    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.scrollEnabled = NO; // 设置不让滑动
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

为自定义 cell 注册到 UICollectionView 中:

[_collectionView registerClass:[GCImageGridCell class] forCellWithReuseIdentifier:kGCImageGridCell];

然后,我们设置 UICollectionView 的数据源协议:

_collectionView.dataSource = self;

接着实现 UICollectionView 的协议内容:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GCImageGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kGCImageGridCell forIndexPath:indexPath];
    NSString *imgName = self.datas[indexPath.row];
    cell.imgView.image = [UIImage imageNamed:imgName];
    return cell;
}

接着,设置前面的 UICollectionViewFlowLayout 的属性,将滑动的方向设置为纵向滑动:

layout.scrollDirection = UICollectionViewScrollDirectionVertical; //设置布局方式为纵向布局

接下来,计算每个 item 的大小、item 之间的间距和行距、每个分组之间的间距:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat horizontal = 16; // 左右边距
CGFloat itemSpace = 6; // item 的间距
CGFloat itemSize = (screenWidth - horizontal * 2 - itemSpace * 2) / 3; // 计算每个 item 的大小
layout.minimumLineSpacing = itemSpace;
layout.minimumInteritemSpacing = itemSpace;
layout.sectionInset = UIEdgeInsetsMake(0, horizontal, 0, horizontal);
layout.itemSize = CGSizeMake(floor(itemSize), floor(itemSize));

注意:计算item大小时使用到了floor函数向下取整,这是防止每行的 item 有超出屏幕而导致换行的问题

效果呈现

在这里插入图片描述

总结

在这篇文章中,我们学习了如何用 UICollectionView 来制作九宫格布局。希望这篇文章能为大家带来帮助,快来关注我的公众号,让我们一起努力!

关于作者

博文作者:GarveyCalvin
公众号:凡人程序猿
微博:https://weibo.com/feiyueharia
博客园:https://www.cnblogs.com/GarveyCalvin
本文版权归作者所有,欢迎转载,但必须保留此段声明,并给出原文链接,谢谢合作!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 首先,我理解你想实现一个九宫格,并且每个格子点击后可以跳转到指定的页面。为了实现这个功能,你可以按照以下步骤进行: 1. 在 wxml 中定义九宫格布局。可以使用 `wx-grids` 来实现,或者自定义样式。 2. 在 js 中定义九宫格的数据。可以使用数组来存储每个格子的信息,例如:图片、标题、跳转链接等。 3. 在 wxml 中使用 `wx:for` 循环渲染九宫格的每个格子,同时绑定点击事件。 4. 在事件处理函数中获取当前点击的格子信息,并根据跳转链接跳转到对应的页面。可以使用 `wx.navigateTo` 或 `wx.switchTab` 等 API 来实现页面跳转。 下面是一个简单的示例代码: ``` // 在 js 中定义九宫格的数据 Page({ data: { grids: [ { image: 'image1.png', title: '格子1', url: '/pages/page1' }, { image: 'image2.png', title: '格子2', url: '/pages/page2' }, { image: 'image3.png', title: '格子3', url: '/pages/page3' }, // ... ] }, // 点击格子时跳转到对应的页面 onGridTap: function (event) { const index = event.currentTarget.dataset.index; const url = this.data.grids[index].url; wx.navigateTo({ url }); } }) // 在 wxml 中渲染九宫格布局和数据 <view class="grid"> <view wx:for="{{ grids }}" wx:key="index" class="grid-item" data-index="{{ index }}" bindtap="onGridTap"> <image src="{{ item.image }}" class="grid-image"></image> <text class="grid-title">{{ item.title }}</text> </view> </view> ``` 希望这个示例对你有所帮助!如果你还有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值