ios - ReactiveSwift/ReactiveCocoa : How to use UIButton disabled styling but not when Action is in progress?

标签 ios reactive-cocoa reactive-swift

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/

相关文章:

ios - 自 Xcode 8.3 更新以来,ReactiveCocoa 错误 "Let ' 值'是私有(private)的,无法从 '@inline(__always)' 函数引用

c# - 使用 UnobservedTaskException 停止 HockeyApp 使我的 Xamarin iOS 应用程序崩溃

ios - 将 Paypal SDK 与原生 iOS 应用集成

objective-c - 组合锁逻辑 - ReactiveCocoa

swift - 使用 Combine 合并先前的值

swift - 有没有一种方法可以发出类似于 combineLatest 的信号,而不需要所有信号都在最初触发?

ios - 我已将我的应用程序从 swift 2.3 升级到 swift 3.0,我在解决有关上传请求的 Alamofire 问题时面临一个大问题

ios - 继承和扩展框架类的问题

swift - 响应式(Reactive) cocoa 尝试图返回类型

ios - 组合 RAC 信号并接收所有值