admin管理员组

文章数量:1397839

I want to use UICollection to display the cards. Each card is a cell; maybe it needs a custom UICollectionViewFlowLayout. How can I do this?

This is my code, But it can not switch cards. I log self.collectionView.contentOffset.x, it is 0. What is wrong?

@interface ArcCollectionViewFlowLayout()

@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) CGSize topItemSize;

@end

static const int kFlowLayoutVisibleItemCount = 7;

@implementation ArcCollectionViewFlowLayout

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

- (CGSize)collectionViewContentSize {
    return self.collectionView.frame.size;
}

- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger itemsCount = [self.collectionView numberOfItemsInSection:0];
  
    if (itemsCount <= 0) {
        return nil;
    }
    
    NSInteger minVisibleIndex = MAX(self.currentPage, 0);
    NSInteger contentOffset = (int)self.collectionView.contentOffset.x;
    NSInteger collectionBounds = (int)self.collectionView.bounds.size.width;
    CGFloat offset = contentOffset % collectionBounds;
    CGFloat offsetProgress = offset / collectionBounds*1.0f;
    CGFloat offsetAngle = offsetProgress / self.itemSize.width * 30;
    NSInteger maxVisibleIndex = MAX(MIN(itemsCount - 1, self.currentPage + kFlowLayoutVisibleItemCount), minVisibleIndex);
    
    NSMutableArray *attributeArray = [NSMutableArray array];
    for (NSInteger i = minVisibleIndex; i<= maxVisibleIndex; i++) {    
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [[self layoutAttributesForItemAtIndexPath:indexPath] copy];
        attributes.center = CGPointMake(collectionBounds/2.0, self.collectionView.bounds.size.height / 2.0);

        if (i == minVisibleIndex) {
            attributes.zIndex = 1000;
            [self rotate:attributes offsetAngle:-90+offsetAngle];
        } else if (i == minVisibleIndex + 1) {
            attributes.zIndex = 1000 + 1;
            [self rotate:attributes offsetAngle:-60+offsetAngle];
        } else if (i == minVisibleIndex + 2) {
            attributes.zIndex = 1000 + 2;
            [self rotate:attributes offsetAngle:-30+offsetAngle];

        } else if (i == minVisibleIndex + 3) {
            attributes.zIndex = 1000 + 3;
            [self rotate:attributes offsetAngle:0+offsetAngle];

        } else if (i == minVisibleIndex + 4) {
            attributes.zIndex = 1000 + 2;
            [self rotate:attributes offsetAngle:30+offsetAngle];

        } else if (i == minVisibleIndex + 5) {
            attributes.zIndex = 1000 + 1;
            [self rotate:attributes offsetAngle:60+offsetAngle];
        } else if (i == minVisibleIndex + 6) {
            attributes.zIndex = 1000;
            [self rotate:attributes offsetAngle:90+offsetAngle];
        }
        attributes.size = self.topItemSize;
        
        [attributeArray addObject:attributes];
    }
    
    return attributeArray;
}

- (void)rotate:(UICollectionViewLayoutAttributes *)attributes offsetAngle:(CGFloat)offsetAngle {
    CGAffineTransform t0 = CGAffineTransformMakeTranslation(0, -attributes.size.height / 2.0);

    CGAffineTransform t1 = CGAffineTransformMakeRotation(angleToRadian(offsetAngle));
    CGAffineTransform t2 = CGAffineTransformMakeTranslation(0, attributes.size.height / 2.0);
    
    CGAffineTransform t = CGAffineTransformConcat(CGAffineTransformConcat(t0, t1), t2);
    attributes.transform = t;
}

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

- (NSInteger)currentPage {
    return MAX(floor(self.collectionView.contentOffset.x / 100), 0);
}

- (CGSize)topItemSize {
    return CGSizeMake(100, 160);
}

I want to implement a half circle UICollectionView.

本文标签: iosCustom UICollectionViewFlowLayout With Half CircleStack Overflow