我有一个UICollectionview,我用水平填充自定义单元格。但是我想做scrollViewDidScroll的事情,但是没有调用UIScrollView委托方法。我错过了什么吗?
编辑: 好吧,我意识到indexPath返回零并突破。新问题是为什么我无法获得正确的值?
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
var visibleRect = CGRect()
visibleRect.origin = containerView.contentOffset
visibleRect.size = newView.frame.size
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath: IndexPath? = containerView.indexPathForItem(at: visiblePoint)
guard let indexPath = visibleIndexPath else { return }
}
编辑:问题是newView.frame.size返回0我使用了containerView的大小而不是scrollViewWillEndDragging我切换到scrollViewDidEndDecelerating。