单击 xcode/swift 时更改按钮的不透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38534573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:04:05  来源:igfitidea点击:

changing opacity of button when clicked xcode / swift

iosswiftxcode

提问by Zach Fuller

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.

我有一个带有图像的 UIButton,默认情况下,当用户按下按钮时,图像的不透明度会降低到 30% 左右。

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

我想知道如何防止这种情况发生以及如何将不透明度设置为我需要的任何值。

回答by Anubhav Singh

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

添加在 viewController 上,如果您想以编程方式更改不透明度和时间延迟。

@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()
}

回答by Pairie Koh

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.

如果你想改变不透明度,你应该使用下面的代码。alpha 基本上是不透明度。您实际上可以更改底部部分时间,例如您希望它变暗的时间,您还可以更改 sender.alpha 值,例如您希望它变暗的程​​度。

@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
}

回答by TomAshTee

Swift 5

斯威夫特 5

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

您可以通过使用DispatchQueue来做到这一点。更重要的是,要使过渡“平滑”,请使用UIView.animate

Just change the alpha parameter.

只需更改 alpha 参数。

@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
        }
    }
}

回答by ggworean

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

添加到 AtWork,如果您想随时以编程方式更改不透明度。

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

回答by Muskaan Agrawal

this is the easiest way to do it

这是最简单的方法

@IBAction func keyPressed(_ sender: UIButton) {

sender.alpha = 0.5

}

回答by Updyhakim Biin

      //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
    }

回答by AtWork

You can just simply set adjustImageWhenHighlightedto No.

您可以简单地将adjustImageWhenHighlighted设置为No

button.adjustsImageWhenHighlighted = NO;

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

如果它对您不起作用,请告诉我。