Patricia
Patricia

Reputation: 87

Swift 4 Button fade in, out

how can I do fade in and out when button is pressed.

I am looking for most simple and best practice Swift 4 solution as possible.

Something like Apple iOS 11 Calculator button fade...

So far I have this...

myButton.setTitleColor(UIColor.red, for: UIControlState.normal)
myButton.backgroundColor=UIColor.gray

myButton.setTitleColor(UIColor.gray, for: UIControlState.highlighted)
myButton.backgroundColor=UIColor.red

I am kinda lost. Many thanks for any answer.

Upvotes: 2

Views: 418

Answers (1)

Jay Patel
Jay Patel

Reputation: 2740

I don't know about fade in/out for calculator but you will get similar effect by this:

Set UIButton type as Custom and set Text Color as White from storyboard.

enter image description here

Set BackgroundColor of UIButton as Tungsten(take e.g. Calculator app)

enter image description here

Create custom class for UIButton and set customButton class to UIButton from Identity Inspector.

class customButton: UIButton {
    override open var isHighlighted: Bool {
        didSet {
            //Set colors for Highlighted & Unhighlighted
            backgroundColor = isHighlighted ? UIColor.lightGray.withAlphaComponent(0.7) : UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1.0)
        }
    }
}

Result:

enter image description here

Upvotes: 4

Related Questions