我有一个设置有图像的UIButton,默认情况下,当用户按下按钮时,图像的不透明度降低到30%左右。
我想知道如何防止这种情况发生,以及如何将不透明度设置为所需的透明度。
最佳答案
如果要以编程方式更改不透明度和时间延迟,请添加到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()
}
关于ios - 单击xcode/swift时更改按钮的不透明度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38534573/