繁体   English   中英

使用自定义流布局更改UICollectionView单元格大小

[英]Change UICollectionView cell size with custom Flow Layout

我有五行单元格,每行包含四个单元格-5x4。 我试图实现几乎像Apple Watch一样的效果。 屏幕中央的行的单元格尺寸应为100x100,其余的返回尺寸为80x80。 滚动时,远离中心的行应变为80x80,进入中心的行应变为100x100。

我已经实现了prepareLayoutlayoutAttributesForElementsInRectlayoutAttributesForItemAtIndexPath

所以现在我有一个100x100单元格的中心行,其余的是80x80。 为了使其动态,我实现了shouldInvalidateLayoutForBoundsChange但是什么也没有发生。

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{

     NSMutableArray *allAttributes = [NSMutableArray arrayWithCapacity:self.layoutInfo.count];

     [self.layoutInfo enumerateKeysAndObjectsUsingBlock:^(NSString *elementIdentifier,
                                                     NSDictionary *elementsInfo,
                                                     BOOL *stop) {
          [elementsInfo enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath,
                                                      UICollectionViewLayoutAttributes *attributes,
                                                      BOOL *innerStop) {
        //     NSLog(@"%f, %f", newBounds.origin.x, newBounds.origin.y);

             if ((newBounds.origin.y < [[UIScreen mainScreen]bounds].size.height/2-50) && (newBounds.origin.y > [[UIScreen mainScreen]bounds].size.height/2+50)) {
                 CGRect frame = attributes.frame;
                 frame.size.width = 100.0f;
                 frame.size.height = 100.0f;
                 attributes.frame = frame;
             }else{
                 CGRect frame = attributes.frame;
                 frame.size.width = 80.0f;
                 frame.size.height = 80.0f;
                 attributes.frame = frame;
             }
             [allAttributes addObject:attributes];
         }];
     }];

     return YES;
}

shouldInvalidateLayoutForBoundsChange:经常被调用(无论何时调整视图大小或滚动视图...),因此您可能不需要在那里做所有的工作。 如果您知道自己的单元格始终具有固定的大小和位置,则只需在此处返回NO

只要实现了prepareLayoutlayoutAttributesForElementsInRectlayoutAttributesForItemAtIndexPath ,那么请确保还实现了collectionViewContentSize以返回内容的总大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM