From 9efd59f8162fdc1dafdde51ea399d2a073ca17d4 Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Mon, 12 Oct 2015 21:47:22 +0800 Subject: [PATCH] Better layout on minicard --- Yep/Base.lproj/Main.storyboard | 4 +- .../MiniCardSkillLayout.swift | 73 ++++++++++++++++++- .../DiscoverNormalUserCell.xib | 2 +- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/Yep/Base.lproj/Main.storyboard b/Yep/Base.lproj/Main.storyboard index 86cc6c83..69e01554 100644 --- a/Yep/Base.lproj/Main.storyboard +++ b/Yep/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -2107,8 +2107,8 @@ - + diff --git a/Yep/Views/Cells/DiscoverCardUser/MiniCardSkillLayout.swift b/Yep/Views/Cells/DiscoverCardUser/MiniCardSkillLayout.swift index 04eb1a2a..f6226c03 100644 --- a/Yep/Views/Cells/DiscoverCardUser/MiniCardSkillLayout.swift +++ b/Yep/Views/Cells/DiscoverCardUser/MiniCardSkillLayout.swift @@ -10,8 +10,77 @@ import UIKit class MiniCardSkillLayout: UICollectionViewFlowLayout { +var scrollUpAction: ((progress: CGFloat) -> Void)? + +let leftEdgeInset: CGFloat = 0 + override func prepareLayout() { - minimumLineSpacing = 5 - minimumInteritemSpacing = 5 + self.minimumLineSpacing = 5 } + + override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { + + let layoutAttributes = super.layoutAttributesForElementsInRect(rect) + // 先按照每个 item 的 centerY 分组 + var rowCollections = [CGFloat: [UICollectionViewLayoutAttributes]]() + + if let layoutAttributes = layoutAttributes { + for attributes in layoutAttributes { + let centerY = CGRectGetMidY(attributes.frame) + + if let rowCollection = rowCollections[centerY] { + var rowCollection = rowCollection + rowCollection.append(attributes) + rowCollections[centerY] = rowCollection + + } else { + rowCollections[centerY] = [attributes] + } + } + } + + // 再调整每一行的 item 的 frame + for (_, rowCollection) in rowCollections { + //let rowItemsCount = rowCollection.count + + // 每一行总的 InteritemSpacing + //let aggregateInteritemSpacing = minimumInteritemSpacing * CGFloat(rowItemsCount - 1) + + // 每一行所有 items 的宽度 + var aggregateItemsWidth: CGFloat = 0 + for attributes in rowCollection { + aggregateItemsWidth += CGRectGetWidth(attributes.frame) + } + + // 计算出有效的 width 和需要偏移的 offset + //let alignmentWidth = aggregateItemsWidth + aggregateInteritemSpacing + //let alignmentOffsetX = (CGRectGetWidth(collectionView!.bounds) - alignmentWidth) / 2 + + // 调整每个 item 的 origin.x 即可 + var previousFrame = CGRectZero + for attributes in rowCollection { + var itemFrame = attributes.frame + + + if CGRectEqualToRect(previousFrame, CGRectZero) { + itemFrame.origin.x = leftEdgeInset + } else { + itemFrame.origin.x = CGRectGetMaxX(previousFrame) + minimumInteritemSpacing + } + + attributes.frame = itemFrame + + + previousFrame = itemFrame + } + } + + return layoutAttributes + } + + override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { + return true + } + + } diff --git a/Yep/Views/Cells/DiscoverNormalUser/DiscoverNormalUserCell.xib b/Yep/Views/Cells/DiscoverNormalUser/DiscoverNormalUserCell.xib index f73a3616..b3ccb63f 100644 --- a/Yep/Views/Cells/DiscoverNormalUser/DiscoverNormalUserCell.xib +++ b/Yep/Views/Cells/DiscoverNormalUser/DiscoverNormalUserCell.xib @@ -1,5 +1,5 @@ - +