UICollectionView: 一種圓形布局

效果圖如下:

效果圖.png

具體代碼

  • 繼承自UICollectionViewLayout

  • LTCircleLayout.h文件

#import <UIKit/UIKit.h>

@interface LTCircleLayout : UICollectionViewLayout
//item尺寸
@property (nonatomic, assign) CGSize itemSize;
//內(nèi)邊距
@property (nonatomic) UIEdgeInsets sectionInset;
@end
  • LTCircleLayout.m文件
#import "LTCircleLayout.h"

@interface LTCircleLayout ()
//屬性數(shù)組
@property (nonatomic, strong) NSMutableArray *attrsArray;
@end

@implementation LTCircleLayout

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        self.attrsArray = [[NSMutableArray alloc] init];
    }
    return _attrsArray;
}

/**
 初始化設(shè)置
 */
- (void)prepareLayout
{
    [super prepareLayout];
    NSInteger rows = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < rows; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }
}

/**
 返回所有的cell的位置組成的數(shù)組, 在這里確定每一個(gè)cell的具體位置
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attrsArray;
}

/**
 這個(gè)方法需要返回indexPath位置對(duì)應(yīng)cell的布局屬性
 collectionView切換layout布局時(shí)必須實(shí)現(xiàn)的方法
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rows = [self.collectionView numberOfItemsInSection:indexPath.section];
    //圓心x
    CGFloat oX = self.collectionView.frame.size.width * 0.5;
    //圓心y
    CGFloat oY = self.collectionView.frame.size.height * 0.5;
    
    CGFloat width = self.collectionView.frame.size.width;
    CGFloat hegiht = self.collectionView.frame.size.height;
    //寬度直徑
    CGFloat widthDismeter = width - self.sectionInset.left - self.sectionInset.right;
    //高度直徑
    CGFloat hegihtDismeter = hegiht - self.sectionInset.top - self.sectionInset.bottom;
    //圓的半徑
    CGFloat raduis = (widthDismeter > hegihtDismeter ? hegihtDismeter : widthDismeter) * 0.5 - (self.itemSize.width > self.itemSize.height ? self.itemSize.height : self.itemSize.width) * 0.5;
    //計(jì)算cell的布局屬性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attrs.size = self.itemSize;
    CGFloat angle = 2 * M_PI / rows * indexPath.item;
    CGFloat centerX = oX + raduis * sin(angle);
    CGFloat centerY = oY + raduis * cos(angle);
    attrs.center = CGPointMake(centerX, centerY);
    
    return attrs;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,192評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,431評(píng)論 4 61
  • 01 你有木有感覺《別把蟑螂放心上》這本書的書名很怪?為啥不要把蟑螂放心上?放在心上會(huì)怎么樣?蟑螂又代表的是什么?...
    牛牛要努力閱讀 2,507評(píng)論 0 2
  • 影影綽綽的余暉穿梭在小巷斑駁的石板墻邊上 沿路結(jié)伴放學(xué)回家的學(xué)生笑顏掛在臉上 學(xué)校門口零售店玻璃...
    取個(gè)帥氣的昵稱拔閱讀 293評(píng)論 0 0
  • 最至幸的莫過于,是戰(zhàn)士,卻重來不用像戰(zhàn)士一樣,不用擋兵、不用掩水,因?yàn)楸澈罂傆幸粋€(gè)人守護(hù)、為你請(qǐng)將運(yùn)土。
    Yesour閱讀 268評(píng)論 0 2

友情鏈接更多精彩內(nèi)容