changing opacity of button when clicked xcode / swift

I have an UIButton set up with an image and by default when the user presses the button, the image is reduced to around 30% opacity.

I am wondering how to prevent this from happening and how to set the opacity to whatever I need it to be.

1

9 Answers

To add on the viewController, if you want to change opacity and time delay programmatically.

@IBAction func keyPressed(_ sender: UIButton) { playSound(soundName: sender.currentTitle!) //Reduces the sender's (the button that got pressed) opacity to half. sender.alpha = 0.5 //Code should execute after 0.2 second delay. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { //Bring's sender's opacity back up to fully opaque. sender.alpha = 1.0 } } func playSound(soundName: String) { let url = Bundle.main.url(forResource: soundName, withExtension: "wav") player = try! AVAudioPlayer(contentsOf: url!) player.play() } 
4

Swift 5

You can do this by using DispatchQueue. What's more, to make the transition "smooth" use UIView.animate.

Just change the alpha parameter.

@IBAction func keyPressed(_ sender: UIButton) { sender.alpha = 0.5 DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 ) { sender.alpha = 1.0 } } 

Smooth change of the parameter.

@IBAction func keyPressed(_ sender: UIButton) { UIView.animate(withDuration: 0.3) { sender.alpha = 0.5 } DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 ) { UIView.animate(withDuration: 0.3) { sender.alpha = 1.0 } } } 

if you want to change the opacity you should use this code below. The alpha is basically the opacity. You can actually change the bottom part time, like how long you want it to be dimmed, you can also change sender.alpha value, like how dim you want it to be.

@IBAction func keyPressed(_ sender: UIButton) { //Reduces the sender's (the button that got pressed) opacity to half. sender.alpha = 0.5 //Code should execute after 0.2 second delay. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { //Bring's sender's opacity back up to fully opaque. sender.alpha = 1.0 } } 
1

To add on to AtWork, if you want to change the opacity programmatically at any time.

button.alpha = 0.30 // Make sure to use CGFloat literals button.alpha = 1 button.alpha = 0 

I don't like a lot of these answers because they shortcut making the user think the button changes color when they tap it but only mimic a functionality apple has already provided for us. In my mind the optimal functionality would allow the button to change its opacity only when pressed and then revert when not selected. Try this out in Swift 5:

1) Create a new swift file called SomeCustomBtn

2) Insert this code:

import UIKit class SomeCustomBtn: UIButton { override open var isHighlighted: Bool { didSet { alpha = isHighlighted ? 0.5 : 1.0 } } } 

3) Add your custom class to your buttons and iOS will automatically change your alpha based on the attribute isHighlighted!

1

Easiest way:

 @IBAction func keyPressed(_ sender: UIButton) { sender.alpha = 0.5 } 
 //Reduces the opacity of the Button to half (the selected Button) sender.alpha = 0.5 //this line of code will help you to delay the opacity to the selected seconds DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { //This code brings sender's opacity back to fully opaque. sender.alpha = 1.0 } 

or one easy way is without using any DispatchQueue is this:

 @IBAction func KeyDownPressed(_ sender: UIButton) { sender.alpha = 0.5 } @IBAction func keyPressed(_ sender: UIButton) { sender.alpha = 1 playSound(col : sender.currentTitle!) } 

Please Note: Set Event of action of func KeyDownPressed to Touch Down

You can just simply set adjustImageWhenHighlighted to No.

button.adjustsImageWhenHighlighted = NO; 

Let me know if it didn't work for you.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like