ios - 单击 xcode/swift 时更改按钮的不透明度
<p><p><strong>我设置了一个带有图像的 UIButton,默认情况下,当用户按下按钮时,图像的不透明度会降低到 30% 左右。</strong> </p>
<p>我想知道如何防止这种情况发生以及如何将不透明度设置为我需要的任何值。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>添加 viewController,如果您想以编程方式更改不透明度和时间延迟。</p>
<pre class="lang-swift prettyprint-override"><code>@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()
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 单击 xcode/swift 时更改按钮的不透明度,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38534573/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38534573/
</a>
</p>
页:
[1]