Trying to override “selected” in UICollectionViewCell Swift for custom selection state

夙愿已清 提交于 2019-11-30 08:05:06

And for Swift 3.0:

override var isSelected: Bool {
    didSet {
        alpha = isSelected ? 0.5 : 1.0
    }
}

Figured it out by stepping into code. The problem was that the super.selected wasn't being modified. So I changed the code to this:

override var selected: Bool {
    get {
        return super.selected
    }
    set {
        if newValue {
            super.selected = true
            self.imageView.alpha = 0.5
            println("selected")
        } else if newValue == false {
            super.selected = false
            self.imageView.alpha = 1.0
            println("deselected")
        }
    }
}

Now it's working.

Try this one.

override var selected: Bool {
    didSet {
        self.alpha = self.selected ? 0.5 : 1.0
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!