首先我要获取布局并计算包含间距的单元格的宽度。之后我要计算当前显示Item的索引。最终结果我希望滚动视图计数并结束下注每当我停止滚动时,但这会给我一个计算错误。 “无法将类型'CGPoint'的值分配给类型'CGFloat'”没有线索解决此问题。需要帮助
这是功能代码
extension HomeViewController : UIScrollViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint,targetContentOffset targetContentOffSet: UnsafeMutablePointer<CGPoint>) {
let layOut = self.collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidthIncludingSpacing = layOut.itemSize.width + layOut.minimumLineSpacing
var offSet = targetContentOffSet.pointee.x
let index = (offSet.x + scrollView.contentInset.left) / cellWidthIncludingSpacing //error Value of type 'CGFloat' has no member 'x'
let roundedIndex = round(visibleWidth)
offSet = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left,y: -scrollView.contentInset.top) //"Cannot assign value of type 'CGPoint' to type 'CGFloat'"
targetContentOffSet.pointee = offSet
}
}
答案 0 :(得分:2)
查看代码的这一行:
var offSet = targetContentOffSet.pointee.x
Swift为offSet
推导什么类型?您可以在Xcode中选择单击它以进行查找,但是我们可以看到它必须是CGPoint.x
的类型,即CGFloat
。
在函数的其余各行中,我想您希望将其键入CGPoint
,因此将其更改为:
var offSet = targetContentOffset.pointee