zl程序教程

您现在的位置是:首页 >  其它

当前栏目

CollectionView旋转水平卡片布局

布局 旋转 水平 卡片
2023-09-14 08:57:57 时间
// 我们必须重写此方法,指定布局大小 // 每次layout invalidated或者重新query布局信息时,会调用 - (void)prepareLayout; // 用于决定布局信息 // 我们必须重写它来实现布局信息 - (nullable NSArray __kindof UICollectionViewLayoutAttributes * *)layoutAttributesForElementsInRect:(CGRect)rect; // 重写它来布局信息 - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
// return YES to cause the collection view to requery the layout for geometry information // 当重新查询布局信息时,就会调用此API。要设置为YES,才能实现自定义布局。 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
  UICollectionViewLayoutAttributes *attribute = [super layoutAttributesForItemAtIndexPath:indexPath];   [self setTransformForLayoutAttributes:attribute];   return attribute; - (NSArray UICollectionViewLayoutAttributes * *)layoutAttributesForElementsInRect:(CGRect)rect {
- (void)setTransformForLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute {
  UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:attribute.indexPath];   if (self.mainIndexPath attribute.indexPath.section == self.mainIndexPath.section) {
  } else if (self.willMoveToMainIndexPath attribute.indexPath.section == self.willMoveToMainIndexPath.section) {

这里主要是要处理旋转。然后要处理切换cell的attribute设置。mainIndexPath属性用于记录当前显示的cell的位置。willMoveToMainIndexPath记录将要出现的cell的位置。

这里在慢慢切换时,效果是挺好的,但是如果快速切换卡片,你会发现会有一点点不好之处,就是下一个cell突然出现的。


hybcoder 运营小帮手新闻推广软文发布推广平台,官网:https://www.yunyingxbs.com