【问题标题】:Button opacity / alpha setting按钮不透明度/alpha设置
【发布时间】:2020-02-19 11:14:59
【问题描述】:

我想在用户按下按钮时更改 alpha。我已经使用了下面的代码并且它有效。 但是,我必须为连接到帮助的每个按钮编写这行代码。

有什么方法可以创建一个方法/类/函数来使所有按钮都具有此功能?

@IBAction func sundayButton(_ sender: UIButton) {
    sender.alpha = 0.5
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {sender.alpha = 1.0

    }
}

【问题讨论】:

  • 如何将此 IBAction 设置/链接到您的其他按钮?因此,每个按钮点击都会触发sundayButton 动作。您还可以检查通过发件人单击了哪个按钮,这已解释为here

标签: swift


【解决方案1】:

您可以在 Button 上编写扩展名并在每个按钮上调用它


extension UIButton {
    func setAlpha() {
        self.alpha = 0.5
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            self.alpha = 1.0
        }
    }
}

这样称呼它:

myButton.setAlpha()

【讨论】:

  • 如果需要也可以添加参数:func setAlpha(from: CGFloat, to: CGFloat) { ... }
猜你喜欢
  • 2014-01-05
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多