UIButton
可以配置为在启用或禁用按钮时使用不同的样式、标题等,例如与 UIButton.setTitle(String, forState:UIControlState)
.
ReactiveCocoa
让我连接一个 ReactiveSwift.Action
到按钮的 reactive.pressed
属性,如果 Action
禁用后,按钮将显示禁用的样式:这太棒了!
但是一个ReactiveSwift.Action
当它有 SignalProducer
时也会被禁用。进行中。这种锁定对于附加到慢速操作(例如网络请求)的 UI 元素很有用,但当操作快速但不是即时时会产生不良的视觉闪烁。
一个简单的解决方法是不使用内置 UIButton
禁用样式:相反,我们可以公开 Property<Bool>
某处并使用它来启用/禁用 Action
并显式更改按钮的样式。然而,这有点笨拙和尴尬,所以我的问题是:是否有一种“干净”的方式来组合 UIButton
通过显式禁用 Action
来禁用按钮的内置禁用样式里面UIButton.reactive.pressed
,不在操作进行时显示禁用样式?
最佳答案
我们最终使用了 application
reserved area in the UIControlState
bitset显式编码:
extension UIControlState {
public static var myActionIsExecuting = UIControlState(rawValue: 1 << 16)
}
public class ReactiveAwareStylingButton: UIButton {
override public var state: UIControlState {
return [super.state, customState]
}
var customState: UIControlState {
if reactive.pressed?.isExecuting.value == true {
return [.myActionIsExecuting]
}
return []
}
}
这样我们就可以使用标准的状态相关样式选项,例如
button.setTitleColor(normalColor, for: .normal)
button.setTitleColor(normalColor, for: [.disabled, .myActionIsExecuting])
button.setTitleColor(disabledColor, for: .disabled)
如果需要,我们仍然可以选择在操作执行时使用禁用的样式(这可能适合缓慢的操作,例如网络请求),只需忽略 .myActionIsExecuting
状态即可.
如果应用此技术,请注意,编译器会很高兴地允许您犯语义错误:您可以将如上所述的样式应用于 UIButton
实例,该实例不是自定义子类ReactiveAwareStylingButton
,它永远不会进入状态.myActionIsExecuting
。您应该防止此错误,例如仅在采用 ReactiveAwareStylingButton
参数的函数中应用样式。
关于ios - ReactiveSwift/ReactiveCocoa : How to use UIButton disabled styling but not when Action is in progress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983762/