NKQuartzClockTimePicker
描述
以圆盘式机械时钟形式的自定义时间组件选择器,具有全调整时钟面显示的可能。
界面
配置
NKQCTimeComponent - 公共枚举;必要以指明可选时间组件的类型
enum NKQCTimeComponent {
case second
case minute
case hour12
case hour24
}
NKQCTimeButtonConfiguration - 结构体,用于设置时钟面时间按钮的显示。
struct NKQCTimeButtonConfiguration {
var textConfig: NKQCTextConfiguration
var sideLength: CGFloat
var backgroundColor: UIColor
var cornerRadius: CGFloat
var borderWidth: CGFloat
var borderColor: CGColor
var size: CGSize {
return CGSize(width: sideLength, height: sideLength)
}
}
NKQCTextConfiguration - 结构体,用于设置文本显示格式。
struct NKQCTextConfiguration {
var fontName: String
var size: CGFloat
var color: UIColor
func font() -> UIFont {
return UIFont(name: fontName, size: size) ?? UIFont.systemFont(ofSize: size)
}
}
NKQCHandConfiguration - 结构体,用于设置时钟手的显示格式。
struct NKQCHandConfiguration {
var color: UIColor
var width: CGFloat
}
NKQCNotchConfiguration - 结构体,用于设置时钟面缺口的显示格式。
struct NKQCNotchConfiguration {
var style: Style
var width: CGFloat
var color: UIColor
var count: UInt?
//style of the notch
public enum Style {
case line
case dashes(CGFloat)
case dots
}
}
在 dashes(CGFloat)
传入的情况下,定义每条划线的长度。
协议
NKQuartzClockTimePickerDataSource - 协议声明了根据指定时间显示按钮及其标题所必需的方法。
@objc protocol NKQuartzClockTimePickerDataSource: class {
/**
Returns BOOL value specifying the need to display a button corresponding to the incoming time
*/
func clockPicker(_ picker: NKQuartzClockTimePicker, shouldAddTitleFor time: Int) -> Bool
/**
Returns String value that specifies the title of the button corresponding to the transmitted time.
If returned value == nil, as value will be setted default String(time)
*/
func clockPicker(_ picker: NKQuartzClockTimePicker, titleFor time: Int) -> String?
}
NKQuartzClockTimePickerDelegate - 协议声明了在选择器状态发生变化时调用的方法。
@objc protocol NKQuartzClockTimePickerDelegate: class {
/**Method is called when time picker value is changed*/
func clockPicker(_ picker: NKQuartzClockTimePicker, didChange time: Int)
}
类
NKQuartzClockTimePicker
class NKQuartzClockTimePicker: UIView {
/**Object that implements protocol NKQuartzClockTimePickerDataSource*/
@IBOutlet weak var dataSource: NKQuartzClockTimePickerDataSource?
/**Object that implements protocol NKQuartzClockTimePickerDelegate*/
@IBOutlet weak var delegate: NKQuartzClockTimePickerDelegate?
/**
Type of selectable time component.
Default value = NKQCTimeComponent.second
*/
var timeComponent: NKQCTimeComponent = .second {
didSet { putTimeButtons() }
}
/**
Current time component value.
Values for seconds and minutes in range 0...59
Values for 12 hours format in range 1...12
Values for 24 hours format in range 0...23
*/
private (set) var time: Int = 0
/**
Indent buttons from the nearest NKQuartzClockTimePicker view side
*/
@IBInspectable var timeButtonsMargin: CGFloat = 0.0 {
didSet { putTimeButtons() }
}
/**
Settings clockface time button display format for normal (unselected) state.
Default value = NKQCTimeButtonConfiguration()
*/
var timeButtonNormalConfig: NKQCTimeButtonConfiguration = NKQCTimeButtonConfiguration() {
didSet { putTimeButtons() }
}
/**
Settings clockface time button display format for selected state.
Default value = NKQCTimeButtonConfiguration()
*/
var timeButtonSelectedConfig: NKQCTimeButtonConfiguration = NKQCTimeButtonConfiguration() {
didSet { putTimeButtons() }
}
/**
Indent notches from the nearest NKQuartzClockTimePicker view side.
*/
@IBInspectable var notchMargin: CGFloat = 0.0 {
didSet { drawNotch() }
}
/**
Array of NKQCNotchConfiguration for combining notches on clockface.
Notche's layers are superimposed in the order of adding to the array
*/
var notches: [NKQCNotchConfiguration]? {
didSet { drawNotch() }
}
/**
Indent of clock hand end from the nearest NKQuartzClockTimePicker view side
*/
@IBInspectable var handMargin: CGFloat = 0.0 {
didSet { drawHand() }
}
/**
Settings clock hand display format.
*/
var hand: NKQCHandConfiguration = NKQCHandConfiguration() {
didSet { drawHand() }
}
/**
Method for set self.time with animation
*/
func setTime(_ value: Int) {
rotate(to: abs(value % timeComponent.count))
}
}
示例
要运行示例项目,请克隆仓库,然后从示例目录首先运行 pod install
。
截图
需求
iOS 10.0 Xcode 9 Swift 4.0
安装
NKQuartzClockTimePicker 通过 CocoaPods 提供。要安装,请将以下行添加到您的 Podfile 中
pod 'NKQuartzClockTimePicker'
作者
Nick Kopilovskii, [email protected]
许可
NKQuartzClockTimePicker 采用 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。