iOS功能列表

1.彈幕:

https://github.com/zkfpk6/YGFlyCommentManager-Danmaku

2.瀑布流:

- (void)prepareLayout; 準備布局cell

先存儲各列MaxY值

再循環(huán)執(zhí)行 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath,設置新的cell布局屬性并存儲

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;? 改變cell布局屬性

已知寬度,讓代理根據(jù)實際內(nèi)容計算高度返回,至此最新的size可得到,然后遍歷MaxY數(shù)組,找出最短的是哪一列, 最后根據(jù)最短列 計算出X 、Y值,設置UICollectionViewLayoutAttributes的frame屬性,并更新MaxY數(shù)組。

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;返回所有的cell布局屬性

返回所有cell的布局屬性(prepareLayout中存儲的)

- (CGSize)collectionViewContentSize;

設置ContentSize

計算出collectionView的contentSize(遍歷MaxY數(shù)組找出最長列即可得到新的高度)。詳細代碼如下@interface RealityShowLayout ()/** 這個字典用來存儲每一列最大的Y值(每一列的高度) */@property (nonatomic, strong) NSMutableDictionary *maxYDict;/** 存放所有的布局屬性 */@property(nonatomic,strong)NSMutableArray *attributeArray;

@end@implementation RealityShowLayout

- (NSMutableDictionary *)maxYDict {? ? ?

? ? ?if (!_maxYDict) {? ?

? ? ? ? ? ? ?self.maxYDict = [[NSMutableDictionary alloc] init];

}?

return _maxYDict;

}

- (NSMutableArray *)attributeArray {

? if (!_attributeArray) {?

? ? ? self.attributeArray = [[NSMutableArray alloc] init];?

}

? ?return _attributeArray;

}

#pragma mark -初始化默認值

- (instancetype)init {

? ?if (self = [super init]) {

? ? ? ? self.columnMargin = 10;

? ? ? ? self.rowMargin = 10;?

? ? ? ?self.columnsCount = 2;?

? ? ? self.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);

}

? ? ?return self;

}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds

{ return YES;}

- (void)prepareLayout {

? [super prepareLayout];? // 1.清空最大的Y值?

? ? for (int i = 0; i<2; i++) {

? ? ?NSString *column = [NSString stringWithFormat:@"%d", I]; ? ? ? ? ? ?self.maxYDict[column] = @(self.sectionInset.top);?

}

[self.attributeArray removeAllObjects]; // 總 item 數(shù)

? NSInteger count = [self.collectionView numberOfItemsInSection:0];

? ?for (int i = 0; i *)layoutAttributesForElementsInRect:(CGRect)rect {? ?

? ? return self.attributeArray;

}

// 計算ContentSize- (CGSize)collectionViewContentSize {? ? // 默認最大Y值在第0列? ? __block NSString *maxColumn = @"0";? ?

?[self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {? ? ? ?

? ?if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {?????????

? ? ? ? ? ? ? ?maxColumn = column;? ? ? ?

}? ? }];? ?

return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);

}


- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {? ? // 1.計算尺寸? ? CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin) / self.columnsCount;? ? // 代理計算傳入高的值? ? CGFloat height = [self.delegate flowLayout:self heightForWidth:width atIndexPath:indexPath];? ? // 2.0假設最短的那一列的第0列? ? __block NSString *minColumn = @"0";? ? // 遍歷字典找出最短的那一列? ? [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {? ? ? ? if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {? ? ? ? ? ? ? ? minColumn = column;? ? ? ? }? ? }];? ? // 2.1計算位置? ? ? ? CGFloat x = self.sectionInset.left + (self.columnMargin + width) * [minColumn intValue];? ? ? ? CGFloat y = [self.maxYDict[minColumn] floatValue]+ _rowMargin;? ? ? ? self.maxYDict[minColumn] = @(y + height);? ? ? ? // 3.創(chuàng)建屬性? ? ? ? UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];? ? ? ? attrs.frame = CGRectMake(x, y, width, height);? ? ? return attrs;}橫向瀑布流原理一樣........此處省略一萬字

3.進度條

https://github.com/AliThink/HorizontalProgress

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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