快速控制
为快速实现基于标准的(启用、高亮、选中)和自定义状态的用户控件子类提供基类。基于 KVC 实现。
[](https://travis-ci.org/Denis Koryttsev/QUIckControl)
安装
快速控制通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行
pod "QUIckControl"
管理状态
您可以为特定的目标绑定类型的状态值
- 简单的状态作为位掩码(UIControlState);
- 当前状态中包含的状态(.intersected);
- 不属于指定状态的所有状态(.inverted);
- 所有状态,其中当前状态包含一个或多个指定的子状态(.oneOfSeveral);
- 所有状态,其中当前状态不包含指定的子状态(.noneOfThis);
- 自定义状态,由你实现(.custom);
所有状态类型都具有从最优先级状态设置的优先级和值。在默认实现中,简单状态优先级为1000,交集状态优先级为999,反转状态优先级为750,oneOfSeveral和noneOfThis优先级为500,自定义用户定义的。你可以为任何类型的州描述设置你的优先级。
示例使用
在大多数情况下,状态看起来像布尔属性,或者它可以表示为布尔属性。所以,在为状态设置值之前,需要使用以下方法进行注册:
func register(_ state: UIControlState, forBoolKeyPath keyPath: String, inverted: Bool)
func register(_ state: UIControlState, with predicate: NSPredicate)
示例
register(.disabled, forBoolKeyPath: #keyPath(UIControl.enabled), inverted: true)
register(.valid, with: NSPredicate { control, _ in
return control.filled && control.valid
})
立即,在注册后,您可以使用以下方法为此状态设置值:
func setValue(_ value: Any?, forTarget: NSObject = default, forKeyPath: String, forInvertedState: UIControlState) {
func setValue(_ value: Any?, forTarget: NSObject = default, forKeyPath: String, forAllStatesContained: UIControlState)
func setValue(_ value: Any?, forTarget: NSObject = default, forKeyPath: String, for: UIControlState)
func setValue(_ value: Any?, forTarget: NSObject = default, forKeyPath: String, for: QUICStateDescriptor)
示例
control.setValue(UIColor.black, forKeyPath: #keyPath(UIView.backgroundColor), forAllStatesContained: .highlighted)
control.setValue("QuickControl sended this string",
forTarget:receiver
forKeyPath: #keyPath(StringReceiver.value),
for: QUICStateDescriptor(state: [.filled, .invalid], priority: 1000, predicate: { $0.contains(.filled) && !$0.contains(.invalid) }))
删除值
func removeValues(forTarget target: NSObject, forKeyPath key: String, forState state: UIControlState)
func removeValues(forTarget target: NSObject, forKeyPath key: String)
func removeValues(forTarget target: NSObject? = default)
对于多开关状态因素(即布尔属性)使用转换
func beginTransition()
func endTransition() // without apply current state
func commitTransition() // with apply current state
func performTransition(withCommit commit: Bool = default, transition: () -> Void)
其他可能性
您可以使用以下方式订阅状态和事件:
func subscribe(on events: UIControlEvents, _ action: @escaping (QUIckControl) -> Void) -> QUIckControlActionTarget
func subscribe(on state: QUICStateDescriptor, _ action: @escaping () -> ())
示例
control.subscribe(on: QUICStateDescriptor(intersected: .valid), { button.enabled = true })
PinCodeControl
QUICKControl子类,用于输入PIN码。它使用编程状态来更改视觉视图。
pod "PinCodeControl"
自定义事件和状态
extension UIControlEvents {
public static var typeComplete = UIControlEvents(rawValue: 1 << 24)
}
extension UIControlState {
public static var filled = UIControlState(rawValue: 1 << 16)
public static var invalid = UIControlState(rawValue: 1 << 17)
public static var valid = UIControlState(rawValue: (1 << 18) | filled.rawValue)
}
// preset state descriptors
enum States {
static public let plain: QUICStateDescriptor
static public let valid: QUICStateDescriptor
static public let invalid: QUICStateDescriptor
static public let highlighted: QUICStateDescriptor
static public let disabled: QUICStateDescriptor
}
主要API
var code: String { get } // entered code
var filled: Bool { get } // enabled, when all code entered
var valid: Bool { get } // disabled, when entered code invalid
var validator: BlockPredicate<String>? // object for user validation pin code value
var shouldUseDefaultValidation: Bool // if true, then code equal strings, such as '1111', '1234', '9876' will be defined as invalid values
var filledItemColor: UIColor? // color for entered code element
var itemPath: UIBezierPath? // bezier path for code element
init(parameters: Parameters, frame: CGRect? = default) // main initializer
func clear() // clear all entered code
// methods for set parameters for each state
func setFillColor(fillColor: UIColor?, for state: QUICStateDescriptor)
func setBorderColor(borderColor: UIColor?, for state: QUICStateDescriptor)
func setBorderWidth(borderWidth: CGFloat, for state: QUICStateDescriptor)
func setForValidState(fillColor: UIColor?, borderColor: UIColor?, borderWidth: CGFloat)
func setForInvalidState(fillColor: UIColor?, borderColor: UIColor?, borderWidth: CGFloat)
func setForPlainState(fillColor: UIColor?, borderColor: UIColor?, borderWidth: CGFloat)
func setForHighlightedState(fillColor: UIColor?, borderColor: UIColor?, borderWidth: CGFloat)
func setForDisabledState(fillColor: UIColor?, borderColor: UIColor?, borderWidth: CGFloat)
// validation
func validate() -> Bool // perform validation current code value
func validate(_ pin: String) -> Bool // method for validation entered pin code. Declared for subclasses override.
不支持Objective C版本。 支持信息
要快速研究此实现的模式,您可以查看项目:https://github.com/k-o-d-e-n/Statable
贡献
作者
Denis Koryttsev,[email protected]
许可证
QUIckControl 在MIT许可下可用。有关更多信息,请参阅LICENSE文件。