自定义流水布局

  • 只有系统达不到我们想要的效果,我们就可以自定义
创建一个类继承UICollectionViewFlowLayout,这个类就是自定义的流水布局(FlowLayout)
重写他的方法,扩展功能

/*
什么时候调用:collectionView第一次布局,collectionView刷新的时候也会调用
作用:计算cell的布局,条件:cell的位置是固定不变的
*/

- (void)prepareLayout{
    [super prepareLayout];
}

/*
作用:计算collectionView滚动范围
*/

- (CGSize)collectionViewContentSize{
    return [super collectionViewContentSize];
}

/*
作用:指定一段区域(rect)给你这段区域内cell的尺寸
可以一次性返回所有cell尺寸,也可以每隔一个距离返回cell
UICollectionViewLayoutAttributes:确定cell的尺寸
一个UICollectionViewLayoutAttributes对象就对应一个cell
拿到UICollectionViewLayoutAttributes就相当于拿到cell
*/

- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSArray *attrs = [super layoutAttributesForElementsInRect:rect];
    int i = 0;
    for (UICollectionViewLayoutAttributes *attr in attrs) {
        
        if (i == 0) {
            attr.transform = CGAffineTransformMakeScale(0.5, 0.5);
        }
        i++;
    }
    return attrs;
}

/*
Invalidate:刷新
作用:在滚动的时候是否允许刷新布局
*/

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return NO;
}

/*
什么时候调用:用户手指一松开就会调用
作用:确定最终偏移量
拖动比较快,最终偏移量不等于手指离开时偏移量
*/

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
    //最终偏移量
    CGPoint targetP = [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity];
    //手指偏移量
    self.collectionView.contentOffset;
    //如果我们返回CGPointZero,我们拖动到哪里,就会自动返回到原点
    return targetP;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iOS开发疯狂者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值