mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-26 05:05:52 +10:00
Better layout on minicard
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ha4-ol-D2e">
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="Ha4-ol-D2e">
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
|
||||
@@ -2107,8 +2107,8 @@
|
||||
<image name="yep_icon_solo" width="120" height="120"/>
|
||||
</resources>
|
||||
<inferredMetricsTieBreakers>
|
||||
<segue reference="0bS-uM-LPI"/>
|
||||
<segue reference="N7F-Tw-oVn"/>
|
||||
<segue reference="o3b-2D-AMq"/>
|
||||
<segue reference="X2e-mz-fLa"/>
|
||||
</inferredMetricsTieBreakers>
|
||||
</document>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
|
||||
<dependencies>
|
||||
<deployment identifier="iOS"/>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
|
||||
|
||||
Reference in New Issue
Block a user