PinCodeControl 0.2.1

PinCodeControl 0.2.1

测试测试中
语言语言 SwiftSwift
许可证 MIT
发布最新发布2019年12月
SPM支持 SPM

Denis Koryttsev 维护。



  • 作者:
  • Denis Koryttsev

QUIckControl

基于标准的(启用,高亮,选中)和自定义状态快速实现 UIControl 子类的基类。基于 KVC 实现的。

Version License Platform

安装

QUIckControl 通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile 中:

pod "QUIckControl"

管理状态

您可以使用特定类型的当前状态绑定值。

  • 简单的状态作为位掩码(UIControlState);
  • 当前状态包含的状态(.intersected);
  • 不匹配指定状态的所有状态(.inverted);
  • 当前状态包含一个或多个指定子状态的所有状态(.oneOfSeveral);
  • 当前状态不包含指定子状态的所有状态(.noneOfThis);
  • 您自己实现的自定义状态(.custom);

所有状态类型都有优先级和值,从最高优先级状态设置。在默认实现中,简单状态优先级为 1000,intersected 为 999,inverted 为 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

快速控制子类,用于输入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]

许可证

快速控制遵守MIT许可证。有关更多信息,请参阅LICENSE文件。