我有一个滑出菜单,已实现为UICollectionViewController。我也为集合视图创建了自定义单元格。导航和一切正常。我遇到的麻烦是在单击实际单元格时更改了单元格的外观。
我已经尝试过基于解决方案(1)(2)的几种方法,但我在这里没有看到满意的结果。
解决方案1 :实现UICollectionViewController委托方法:
class SlideOutMenuViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
//Setup code and other delegate methods….
override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SlideOutMenuCells
cell.backgroundColor = .white
}
override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SlideOutMenuCells
cell.backgroundColor = UIColor.mainGreen()
}
}
当我尝试此解决方案时,没有任何反应。单元格的背景色不会改变颜色。
解决方案2 :此解决方案可产生更好的结果,但当我握住单元格时,该单元格会更改颜色。我希望单元格的背景颜色在 点击 上快速闪烁或突出显示,而不仅仅是让用户按住单元格。
class SlideOutMenuCells: UICollectionViewCell {
//Setup code...
override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
backgroundColor = UIColor.darkGreen()
} else {
backgroundColor = UIColor.mainGreen()
}
}
}
}
这两种解决方案都无法真正按预期工作。我在这里看到了几篇尝试解决此问题的帖子,但还没有找到一种真正有效的解决方案。我希望该单元格通过轻击而不是在用户单击并按住某个单元格时闪烁高亮显示。
答案 0 :(得分:7)
此处是用于突出显示的工作代码 UICollectionViewCell
点击(快速键4)
不需要在您的UICollectionViewCell
类中做任何事情。
class StoreListViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
var previousSelected : IndexPath?
var currentSelected : Int?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoreCollViewCell", for: indexPath) as! StoreCollViewCell
cell.lblofferscount.text = "Cell \(indexPath.row)"
// To set the selected cell background color here
if currentSelected != nil && currentSelected == indexPath.row
{
cell.backgroundColor = UIColor.green
}
else
{
cell.backgroundColor = UIColor.yellow
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// For remove previously selection
if previousSelected != nil{
if let cell = collectionView.cellForItem(at: previousSelected!){
cell.backgroundColor = UIColor.yellow
}
}
currentSelected = indexPath.row
previousSelected = indexPath
// For reload the selected cell
self.collVwStores.reloadItems(at: [indexPath])
}
}
输出
答案 1 :(得分:1)
您可以尝试使用UILongPressGestureRecognizer
来表示选择:
override func awakeFromNib() {
super.awakeFromNib()
let tapGesture = UILongPressGestureRecognizer(target: self, action: #selector(didTap(recognizer:)))
tapGesture.minimumPressDuration = 0
tapGesture.cancelsTouchesInView = false
addGestureRecognizer(tapGesture)
}
@objc func didTap(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == .began {
backgroundColor = .red
}
if recognizer.state == .ended {
backgroundColor = .green
}
}
或者您可以将extension
设为UICollectionViewCell
extension UICollectionViewCell {
func makeSelectionIndicatable() {
let tapGesture = UILongPressGestureRecognizer(target: self, action: #selector(didTap(recognizer:)))
tapGesture.minimumPressDuration = 0
tapGesture.cancelsTouchesInView = false
addGestureRecognizer(tapGesture)
}
@objc private func didTap(recognizer: UILongPressGestureRecognizer) {
if recognizer.state == .began {
backgroundColor = .red
}
if recognizer.state == .ended {
backgroundColor = .green
}
}
}
之后,对于awakeFromNib()
方法中的任何单元格,只需添加makeSelectionIndicatable()
答案 2 :(得分:0)
如果在单元格中查看内容,您可以通过更改颜色来实现:
class SlideOutMenuCells: UICollectionViewCell {
override var isSelected: Bool {
didSet {
self.contentView.backgroundColor = isSelected ? OLTheme.Colors.Selected_Voucher_Color : UIColor.clear
}
}
}
答案 3 :(得分:0)
我遇到了完全相同的问题,解决方案实际上比上面发布的解决方案简单得多。
在视图控制器中,添加collectionView.delaysContentTouches = false
。
然后单元格中的其他代码保持原样:
lass SlideOutMenuCells: UICollectionViewCell {
//Setup code...
override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
backgroundColor = UIColor.darkGreen()
} else {
backgroundColor = UIColor.mainGreen()
}
}
}
}
但是现在讨厌的延迟已经过去了!