点击时改变按钮的不透明度xcode / swift

时间:2016-07-22 20:12:17

标签: ios swift xcode

我知道这是一个非常愚蠢的问题,但我刚刚开始 iOS开发。

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

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

9 个答案:

答案 0 :(得分:9)

如果要更改不透明度,应在下面使用此代码。 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
    }
}

答案 1 :(得分:8)

快捷键5

您可以使用 DispatchQueue 来做到这一点。此外,要使过渡“ 平滑”,请使用 UIView.animate

只需更改alpha参数。

@IBAction func keyPressed(_ sender: UIButton) {

    sender.alpha = 0.5

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 ) {
        sender.alpha = 1.0
    }
}

参数的平滑更改。

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

答案 2 :(得分:2)

最简单的方法:

    @IBAction func keyPressed(_ sender: UIButton) {
        sender.alpha = 0.5
    }

答案 3 :(得分:1)

如果要以编程方式更改不透明度和时间延迟,请添加到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()
}

答案 4 :(得分:1)

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

答案 5 :(得分:1)

或者一种简单的方法是不使用任何DispatchQueue

    @IBAction func KeyDownPressed(_ sender: UIButton) {
        sender.alpha = 0.5
    }

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

请注意:将func KeyDownPressed的动作事件设置为Touch Down

答案 6 :(得分:1)

我不喜欢这些答案,因为它们快捷方式使用户认为他们在点击按钮时会改变颜色,而只是模仿苹果已经为我们提供的功能。在我看来,最佳功能将允许按钮仅在按下时更改其不透明度,然后在未选择时恢复其不透明度。在Swift 5中尝试一下:

1)创建一个名为SomeCustomBtn

的新快速文件

2)插入以下代码:

import UIKit

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

3)将自定义类添加到按钮,iOS会根据属性isHighlighted自动更改您的Alpha!

答案 7 :(得分:0)

您只需将 adjustImageWhenHighlighted 设置为

button.adjustsImageWhenHighlighted = NO;

如果它不适合你,请告诉我。

答案 8 :(得分:0)

如果要在任何时候以编程方式更改不透明度,请添加到AtWork。

button.alpha = 0.30 // Make sure to use CGFloat literals
button.alpha = 1
button.alpha = 0
相关问题