我面临一个奇怪的问题,我似乎无法弄清或发现有关在线的任何信息。
因此,我尝试使用UICollectionView
和自定义UICollectionViewFlowlayout
复制Shazam发现UI。
到目前为止,一切工作都很好,但是当我添加“纸牌堆叠”效果时(或者说是实现它的人),我注意到在某些情况下(或者说,当特定的索引可见时,在示例中的第5、9行)将有4个可见单元,而不是3。我的猜测是这与单元重用有关,但是我不确定为什么它正在这样做。我调查了各个单元的尺寸,它们看起来都一样,因此并不是单元的大小不同。
有人知道为什么会发生这种情况吗?任何帮助或建议都非常感谢。
我将在下面添加自定义流程图和屏幕截图的代码段。 您可以download the full project here,也可以签出the PR on Github。
以下是视觉比较:
自定义布局的源代码:
import UIKit
/// Custom `UICollectionViewFlowLayout` that provides the flowlayout information like paging and `CardCell` movements.
internal class VerticalCardSwiperFlowLayout: UICollectionViewFlowLayout {
/// This property sets the amount of scaling for the first item.
internal var firstItemTransform: CGFloat?
/// This property enables paging per card. The default value is true.
internal var isPagingEnabled: Bool = true
/// Stores the height of a CardCell.
internal var cellHeight: CGFloat!
internal override func prepare() {
super.prepare()
assert(collectionView!.numberOfSections == 1, "Number of sections should always be 1.")
assert(collectionView!.isPagingEnabled == false, "Paging on the collectionview itself should never be enabled. To enable cell paging, use the isPagingEnabled property of the VerticalCardSwiperFlowLayout instead.")
}
internal override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let items = NSMutableArray (array: super.layoutAttributesForElements(in: rect)!, copyItems: true)
items.enumerateObjects(using: { (object, index, stop) -> Void in
let attributes = object as! UICollectionViewLayoutAttributes
self.updateCellAttributes(attributes)
})
return items as? [UICollectionViewLayoutAttributes]
}
// We invalidate the layout when a "bounds change" happens, for example when we scale the top cell. This forces a layout update on the flowlayout.
internal override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
// Cell paging
internal override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
// If the property `isPagingEnabled` is set to false, we don't enable paging and thus return the current contentoffset.
guard isPagingEnabled else {
let latestOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
return latestOffset
}
// Page height used for estimating and calculating paging.
let pageHeight = cellHeight + self.minimumLineSpacing
// Make an estimation of the current page position.
let approximatePage = self.collectionView!.contentOffset.y/pageHeight
// Determine the current page based on velocity.
let currentPage = (velocity.y < 0.0) ? floor(approximatePage) : ceil(approximatePage)
// Create custom flickVelocity.
let flickVelocity = velocity.y * 0.4
// Check how many pages the user flicked, if <= 1 then flickedPages should return 0.
let flickedPages = (abs(round(flickVelocity)) <= 1) ? 0 : round(flickVelocity)
// Calculate newVerticalOffset.
let newVerticalOffset = ((currentPage + flickedPages) * pageHeight) - self.collectionView!.contentInset.top
return CGPoint(x: proposedContentOffset.x, y: newVerticalOffset)
}
internal override func finalLayoutAttributesForDisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
// make sure the zIndex of the next card is higher than the one we're swiping away.
let nextIndexPath = IndexPath(row: itemIndexPath.row + 1, section: itemIndexPath.section)
let nextAttr = self.layoutAttributesForItem(at: nextIndexPath)
nextAttr?.zIndex = nextIndexPath.row
// attributes for swiping card away
let attr = self.layoutAttributesForItem(at: itemIndexPath)
return attr
}
/**
Updates the attributes.
Here manipulate the zIndex of the cards here, calculate the positions and do the animations.
- parameter attributes: The attributes we're updating.
*/
fileprivate func updateCellAttributes(_ attributes: UICollectionViewLayoutAttributes) {
let minY = collectionView!.bounds.minY + collectionView!.contentInset.top
let maxY = attributes.frame.origin.y
let finalY = max(minY, maxY)
var origin = attributes.frame.origin
let deltaY = (finalY - origin.y) / attributes.frame.height
let translationScale = CGFloat((attributes.zIndex + 1) * 10)
// create stacked effect (cards visible at bottom
if let itemTransform = firstItemTransform {
let scale = 1 - deltaY * itemTransform
var t = CGAffineTransform.identity
t = t.scaledBy(x: scale, y: 1)
t = t.translatedBy(x: 0, y: (translationScale + deltaY * translationScale))
attributes.transform = t
}
origin.x = (self.collectionView?.frame.width)! / 2 - attributes.frame.width / 2 - (self.collectionView?.contentInset.left)!
origin.y = finalY
attributes.frame = CGRect(origin: origin, size: attributes.frame.size)
attributes.zIndex = attributes.indexPath.row
}
}
编辑1:作为进一步的说明,最终结果应该使它看起来像这样:
修改2: 从您的测试中看,您每滚动4-5张纸牌似乎就会发生一次。
答案 0 :(得分:2)
您有一个继承自流布局的布局。您将layoutAttributesForElements(in rect:)
覆盖到super.layoutAttributesForElements
,从updateCellAttributes
中提取所有元素,然后为每个元素修改方法UICollectionViewFlowLayout
中的属性。
通常,这是制作流布局的子类的好方法。 updateCellAttributes
正在完成大部分艰苦的工作-弄清楚每个元素应该在哪里,哪些元素在矩形中,它们的基本属性是什么,应该如何填充等等,您可以修改几个“辛苦”工作完成后的属性。当您添加旋转或不透明度或其他不更改项目位置的功能时,此方法效果很好。
使用rows = []
for period in data:
for k in data[period]:
rows.append({'username': k['username'], 'joined':k['joined'], 'period': period})
df = pd.DataFrame(rows)
更改项目框架会遇到麻烦。然后,您可能会遇到以下情况:对于常规流布局,该单元格根本不会出现在框架中,但是现在应该由于您的修改而出现。因此,super.layoutAttributesForElements(在rect:CGRect中)根本不会返回该属性,因此它们根本不会出现。您还可能遇到相反的问题,即根本不应该在框架中的单元格在视图中,但是以用户无法看到的方式进行变换。
您还没有足够解释您想做什么,以及为什么您认为从UIFlowLayout继承对我来说是正确的,因此能够专门为您提供帮助。但我希望我已给您足够的信息,以便您可以自行找到问题。
答案 1 :(得分:1)
该错误是关于如何为每个属性定义frame.origin.y
的。更具体地说,您在minY
中持有的值并确定您在屏幕上保留的单元格数量。 (我将编辑此答案并在以后进行解释,但现在,请尝试替换以下代码)
var minY = collectionView!.bounds.minY + collectionView!.contentInset.top
let maxY = attributes.frame.origin.y
if minY > attributes.frame.origin.y + attributes.bounds.height + minimumLineSpacing + collectionView!.contentInset.top {
minY = 0
}