gpt4 book ai didi

ios - UIButton 在点击时更改背景(以编程方式)

转载 作者:行者123 更新时间:2023-11-30 11:17:45 30 4
gpt4 key购买 nike

我有一个自定义 UIButton,但我无法通过快速点击来更改背景或文本颜色。如果某些东西有效,则只有长按才能实现:

    buton.isUserInteractionEnabled = true
buton.setTitle("text_normal", for: .normal)
buton.setTitle("text_highlighted", for: .highlighted)
buton.setTitle("text_selected", for: .selected)
buton.setTitle("text_focused", for: .focused)

按住按钮约 1 秒后,我可以获得的唯一文本是“text_highlighted”。但短按一下什么也不会发生。

但是 Action 被正确触发:

let tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap))
buton.addGestureRecognizer(tap2)


@objc func handleTap(sender: UITapGestureRecognizer) {
print("click")
}

我尝试过的:添加自定义

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
...

这也是稍后触发的。并且与 tocuhesEnded 结合也暂时没有改变颜色(可能太快了)。

我也无法使用 UIButton(type: .system) - 它不是系统样式的按钮。

与更改文本类似,这也不起作用:

    self.setTitleColor(UIColor.gray, for: .normal)
self.setTitleColor(UIColor.yellow, for: .selected)
self.setTitleColor(UIColor.yellow, for: .highlighted)
self.setTitleColor(UIColor.yellow, for: .focused)

只有长按时我才能看到黄色。我正在寻找类似的东西,但它也需要快速点击才能工作。

自定义按钮正在修改layoutSubviews(),但不修改颜色。自定义按钮包含默认图像和标签。整个按钮都有圆角。但总的来说,里面没有什么特别的。

我没有使用任何 Storyboard或 XIB - 一切都以编程方式在 Swift 4 中。该按钮应该通向另一个 ViewController,但我想看到单击后的即时反馈。就像从 Storyboard创建时一样:https://youtu.be/lksW12megQg?t=3m25s - 现在连简单的 alpha 变化都不适合我。

最佳答案

检查 uibutton 的 isselected 属性,如果选择了按钮,则更改按钮的背景颜色

关于ios - UIButton 在点击时更改背景(以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591338/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com