Knob
Swift 中的旋钮 UIControl。
用法
初始化
let knob = Knob(frame: CGRect(x: (view.bounds.width - 240) / 2,
y: (view.bounds.height - 240) / 2,
width: 240,
height: 240))
view.addSubview(knob)
操作
- 设置值
knob.setValue(targetValue, animated: true)
- 添加目标
knob.addTarget(self, action: #selector(ViewController.handleValueChanged(_:)), for: .valueChanged)
- 处理值变化
@objc func handleValueChanged(_ sender: Knob) {
print("value: \(sender.value)")
}
配置
- 值范围
knob.minimumValue = 0
knob.maximumValue = 260
- 角度范围
knob.startAngle = -CGFloat.pi * 5 / 4
knob.endAngle = CGFloat.pi / 4
- 刻度
knob.graduations = 26
knob.largeGraduations = 13
- 值显示
knob.valueDisplay = .forLargeGraduation
knob.valueAttributes = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 16) ]
knob.valueFormatter = { "\(Int($0))" }
- 指示器类型
knob.indicatorType = .pointer(color: .red, length: 100, width: 4)
- 其他配置
knob.isEnableFeedback = true
knob.isGraduationsAligned = true
knob.isContinuous = true
特性
- 多个指示器
- 控制的角范围
- 显示刻度
- 支持故事大纲
- 自定义指示器层
要求
- iOS 10.0+
- Swift 5.0+
安装
CocoaPods
pod 'Knob', '~> 0.0.1'
Swift 包管理器
dependencies: [
.package(url: "https://github.com/dongyaxun/Knob.git", .upToNextMajor(from: "0.0.1"))
]
许可证
Knob 在 MIT 许可下发布。有关详细信息,请参阅 LICENSE。