Change colour of dark grey highlight when holding down custom UIButton?

前端 未结 5 1172
忘了有多久
忘了有多久 2021-02-02 07:56

I have a custom UIButton which is a cloud, transparent black and white .png file, with no down state, just one image. When tapping and holding the finger over it, i

5条回答
  •  青春惊慌失措
    2021-02-02 08:18

    You can write a custom button that does it

    class ActionButton: UIButton {
    
      var originalBackgroundColor: UIColor!
    
      override var backgroundColor: UIColor? {
        didSet {
          if originalBackgroundColor == nil {
            originalBackgroundColor = backgroundColor
          }
        }
      }
    
      override var isHighlighted: Bool {
        didSet {
          guard let originalBackgroundColor = originalBackgroundColor else {
            return
          }
    
          backgroundColor = isHighlighted ? originalBackgroundColor.darken() : originalBackgroundColor
        }
      }
    

提交回复
热议问题