简体   繁体   English

敲击(以编程方式)时,UIButton更改背景

[英]UIButton change background when tapped (programmatically)

I have a custom UIButton but I am not able to make changing background or text color based on a quick tap. 我有一个自定义的UIButton,但是无法基于快速点击来更改背景或文本颜色。 If something works, it's only on long press: 如果某些方法有效,请长按:

    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)

The only text I can get is "text_highlighted" after holding the button ~1 second. 按住按钮约1秒钟后,我只能获得“ text_highlighted”文本。 But nothing happens on short tap. 但是,短按一无所获。

But the action is triggered correctly: 但是该操作已正确触发:

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


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

What I tried: Adding custom 我试过的:添加自定义

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

which is also triggered later. 稍后也会触发。 And combining with tocuhesEnded didn't change the color temporarily as well (maybe it was too quick). 并且与tocuhesEnded结合使用也不会暂时改变颜色(也许太快了)。

I can't use UIButton(type: .system) as well - it's not system styled button. 我也不能使用UIButton(type:.system)-它不是系统样式的按钮。

Similar as with changing text this is not working as well: 与更改文本类似,此方法也不起作用:

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

I can see yellow only when long pressed. 我只有长按才能看到黄色。 I am looking for something like this, but it needs to work on quick tap as well. 我正在寻找类似的东西,但是它也需要快速点击。

Custom button is modifying layoutSubviews(), but not colors. 自定义按钮正在修改layoutSubviews(),但不能修改颜色。 Custom button contains default image and label. 自定义按钮包含默认图像和标签。 Whole button has rounded corners. 整个按钮都有圆角。 But overall nothing special is in there. 但是总体上没有什么特别的。

I am not using any storyboard or XIB - everything is in Swift 4 programatically. 我没有使用任何情节提要或XIB-所有内容均以编程方式在Swift 4中提供。 The button is supposed to lead to another ViewController, but I want to see the immediate feedback on click. 该按钮应该指向另一个ViewController,但是我想查看单击时的立即反馈。 Something like when created from storyboard: https://youtu.be/lksW12megQg?t=3m25s - not even simple alpha change works for me right now. 从情节提要中创建时类似的东西: https : //youtu.be/lksW12megQg? t = 3m25s-现在即使是简单的alpha更改也不适用于我。

检查uibutton的isselected属性是否被选中,然后更改按钮的背景色

Jen Jose was right - there was a problem with my parent class which was 'eating up' my touches. 詹·何塞(Jen Jose)是对的-我的上课时遇到了一个问题,就是“吃光了我的一切”。 I confirmed this when moving it to different ViewController + I had the same issue with table, which couldn't handle touch events ( https://stackoverflow.com/a/9248827/1317362 ) 我将其移至其他ViewController时已确认这一点+表存在相同的问题,无法处理触摸事件( https://stackoverflow.com/a/9248827/1317362

EDIT: 编辑:

To be precise - this button was in a UIScrollView. 确切地说-此按钮位于UIScrollView中。

Adding scrollView.delaysContentTouches = NO; 添加scrollView.delaysContentTouches = NO; solved the issue completely. 彻底解决了这个问题。

Details: https://stackoverflow.com/a/16650610/1317362 详细信息: https : //stackoverflow.com/a/16650610/1317362

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM