对于您正在使用的库。在您的单元格中添加一个背景图像,该图像将显示为与您相同的大小collectionView
并hidden
默认设置。那么您需要在您的scrollViewDidScroll
方法中应用逻辑并显示位于中心的单元格的背景图像,例如:
let indexPath = IndexPath(item: currentIndex, section: 0)
if let cell = wheelMenuCollectionView.cellForItem(at: indexPath) as? WheelMenuCollectionViewCell {
cell.backImage.isHidden = false
}
并且要删除以前的单元格背景图像,您需要添加
for (index, _) in items.enumerated() {
if index != currentIndex {
let oldIndexPath = IndexPath(item: index, section: 0)
if let cell = wheelMenuCollectionView.cellForItem(at: oldIndexPath) as? WheelMenuCollectionViewCell {
cell.backImage.isHidden = true
}
}
}
你的scrollViewDidScroll
方法看起来像:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let maxOffset = scrollView.bounds.width - scrollView.contentSize.width
let maxIndex = CGFloat(self.items.count - 1)
let offsetIndex = maxOffset / maxIndex
let currentIndex = Int(round(-scrollView.contentOffset.x / offsetIndex)).clamped(to: (0 ... self.items.count-1))
if self.items[currentIndex].id != self.selectedItem.id {
self.selectedItem = self.items[currentIndex]
}
let indexPath = IndexPath(item: currentIndex, section: 0)
if let cell = wheelMenuCollectionView.cellForItem(at: indexPath) as? WheelMenuCollectionViewCell {
cell.backImage.isHidden = false
}
for (index, _) in items.enumerated() {
if index != currentIndex {
let oldIndexPath = IndexPath(item: index, section: 0)
if let cell = wheelMenuCollectionView.cellForItem(at: oldIndexPath) as? WheelMenuCollectionViewCell {
cell.backImage.isHidden = true
}
}
}
}
现在显示当用户启动您需要添加的应用程序时突出显示的第一个单元格
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
let indexPath = IndexPath(item: 0, section: 0)
if let cell = self.wheelMenuCollectionView.cellForItem(at: indexPath) as? WheelMenuCollectionViewCell {
cell.backImage.isHidden = false
}
})
在你的viewWillAppear
方法中。
查看此示例项目以获取更多信息。