0

I have made a button by using PaintCodes code:

class AddIconView: OUIButton {
    override func draw(_ rect: CGRect) {
        AddIcon.draw(frame: rect)
   }
}

Then I add its class to a UIButton. The problem is, the button is not highlighted anymore after touch it, what is the best way to add a highlight to it? for example for this button:

@IBOutlet weak var addButton: AddIconView!

Thank you so much for your help in advance

Edit:

I created a custom class for it:

class OUIButton: UIButton {
override var isHighlighted: Bool {
    get {
        return super.isHighlighted
    }
    set {
        if newValue {
            backgroundColor = .green
        }
        else {
            backgroundColor = .blue
        }
        super.isHighlighted = newValue
    }
 }}

For the test, I added a blue and green color. With this code, when I touch the button the background will become and stay blue. I want it to change only when it is touched and after release, it comes back to the normal state, exactly like a normal UIbutton

0

It's solved, here is the code for anyone who want to use:

class OUIButton: UIButton {
    override var isHighlighted: Bool {
        get {
            return super.isHighlighted
        }
        set {
            if newValue {
                    UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseIn , animations: {
                self.alpha = 0.5
            }, completion: nil)
        }
        else {
            UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseOut , animations: {
                self.alpha = 1
            }, completion: nil)
        }
        super.isHighlighted = newValue
    }
}}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.