Where to highlight UICollectionViewCell: delegate or cell?

后端 未结 7 2567
渐次进展
渐次进展 2020-12-13 09:43

According to the Collection View Programming Guide one should handle the visual state of the cell highlights in the UICollectionViewDelegate. Like this:

7条回答
  •  难免孤独
    2020-12-13 10:21

    Swift 3: (based on the answer of A-Live)

    import UIKit
    
    class MyCollectionViewCell: UICollectionViewCell {
    
        override var highlighted: Bool {
            didSet {
                self.setNeedsDisplay()
            }
        }
    
        override func drawRect(rect: CGRect) {
            super.drawRect(rect)
            myImageView.highlighted = self.highlighted
        }
    }
    

    Swift 4

    import UIKit
    
    class MyCollectionViewCell: UICollectionViewCell {
    
        override var isHighlighted: Bool {
            didSet {
                self.setNeedsDisplay()
            }
        }
    
        override func draw(_ rect: CGRect) {
            super.draw(rect)
            myImageView.isHighlighted = self.isHighlighted
        }
    }
    

提交回复
热议问题