1.弹幕:
https://github.com/zkfpk6/YGFlyCommentManager-Danmaku
2.瀑布流:
- (void)prepareLayout; 准备布局cell
先存储各列MaxY值
再循环执行 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath,设置新的cell布局属性并存储
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath; 改变cell布局属性
已知宽度,让代理根据实际内容计算高度返回,至此最新的size可得到,然后遍历MaxY数组,找出最短的是哪一列, 最后根据最短列 计算出X 、Y值,设置UICollectionViewLayoutAttributes的frame属性,并更新MaxY数组。
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;返回所有的cell布局属性
返回所有cell的布局属性(prepareLayout中存储的)
- (CGSize)collectionViewContentSize;
设置ContentSize
计算出collectionView的contentSize(遍历MaxY数组找出最长列即可得到新的高度)。详细代码如下@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 数
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.创建属性 UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.frame = CGRectMake(x, y, width, height); return attrs;}横向瀑布流原理一样........此处省略一万字
3.进度条
https://github.com/AliThink/HorizontalProgress