iOS UICollectionView 卡片效果 传送带效果 Carousel FlowLayout

本文介绍了如何在iOS应用中创建一个类似于影院选座的卡片效果,使用UICollectionView实现传送带滚动,并详细讲解了缩放效果和滚动结束时定位中间元素的实现细节。还提供了Demo和集成使用的指南。
摘要由CSDN通过智能技术生成

一、场景

在我们的app中需要一个类似影院传送带式选择电影场次的控件,效果如下:
CarouselCenter.gif

二、实现思路

  1. 控件选择
    看控件特征,是一个可滚动的长列表,在iOS中一般都使用UICollectionView来展现,这里我们也选择它。
  2. 布局选择
    UICollectionView中每个item的显示样式都通过UICollectionViewLayout来控制,这里明显是一个“流式布局”,我们可以选择UICollectionViewFlowLayout来定制样式。
  3. 布局控制
    在上面的显示效果中,我们需要控制两个点:
    • 缩放效果
      // 该方法指定UICollectionView的每个item滚动到相应rect的显示效果(UICollectionViewLayoutAttributes,包含尺寸、透明度等信息)
      open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
      
    • 滚动结束,定位到中间位置
      // 该方法指定UICollectionView滚动到的目标位置
      // a layout can return the content offset to be applied during transition or update animations
      open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint
      

三、实现细节

  • 缩放效果
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    guard let superAttributes = super.layoutAttrib
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值