我设置了一个带有图像的 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |