为 CollectionView 的单独 Cell 设置圆形标签 [Swift]
Set a circle label for a separate Cell of CollectionView [Swift]
我正在尝试为 CollectionView 的可见单元格绘制一个圆圈,如下所示
我尝试通过 addSubview
然后 removeFromSuperview
之前的标签,但它不起作用
let myIndex1 = IndexPath(row: 0, section: 0)
let myIndex2 = IndexPath(row: 1, section: 0)
if indexPath.row == 0 {
collectionView.cellForItem(at:myIndex1)?.addSubview(labelNew)
labelNew.layer.backgroundColor = selectedItem.title.cgColor
}
if indexPath.row == 1 {
labelNew.removeFromSuperview()
collectionView.cellForItem(at:myIndex2)?.addSubview(labelNew2)
labelNew2.layer.backgroundColor = selectedItem.title.cgColor
}
在当前位于中心的 CollectionView 的单元格周围绘制圆圈的正确方法是什么?
对于您正在使用的library。在您的单元格中添加一个背景图片,该图片将显示为与您的 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
方法中。
查看 THIS 示例项目以获取更多信息。
我正在尝试为 CollectionView 的可见单元格绘制一个圆圈,如下所示
我尝试通过 addSubview
然后 removeFromSuperview
之前的标签,但它不起作用
let myIndex1 = IndexPath(row: 0, section: 0)
let myIndex2 = IndexPath(row: 1, section: 0)
if indexPath.row == 0 {
collectionView.cellForItem(at:myIndex1)?.addSubview(labelNew)
labelNew.layer.backgroundColor = selectedItem.title.cgColor
}
if indexPath.row == 1 {
labelNew.removeFromSuperview()
collectionView.cellForItem(at:myIndex2)?.addSubview(labelNew2)
labelNew2.layer.backgroundColor = selectedItem.title.cgColor
}
在当前位于中心的 CollectionView 的单元格周围绘制圆圈的正确方法是什么?
对于您正在使用的library。在您的单元格中添加一个背景图片,该图片将显示为与您的 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
方法中。
查看 THIS 示例项目以获取更多信息。