ValueStepper
描述
ValueStepper
是 Apple 的 UIStepper
对象的改进复制。问题是 UIStepper
没有为用户显示值。我创建简单的 UILabel
来在 UI 中显示值已经烦了。《ValueStepper》在增加和减少按钮之间集成了值与 UILabel
。就是这么简单。
使用方法
要查看它的操作,请运行示例项目,克隆仓库,然后首先从 Example
目录运行 pod install
。示例项目展示了如何在 Storyboard 中设置 ValueStepper
。
Storyboard
拖动一个 UIView
对象,将其类设置为 ValueStepper
(如需,也将模块设置为 ValueStepper
)。现在您可以在 IB 中自定义所有属性,如 minimumValue
、tintColor
等。请确保将宽度设置为 149 且高度设置为 29,这是默认值。在 ValueChanged
控制事件上创建一个 @IBAction
,以便在值发生变化时接收到通知。
编程方式
import ValueStepper
let valueStepper: ValueStepper = {
let stepper = ValueStepper()
stepper.tintColor = .whiteColor()
stepper.minimumValue = 0
stepper.maximumValue = 1000
stepper.stepValue = 100
return stepper
}()
override func viewDidLoad() {
super.viewDidLoad()
valueStepper.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
}
@IBAction func valueChanged1(sender: ValueStepper) {
// Use sender.value to do whatever you want
}
自定义
以下是可用的属性及其相关文档。
/// Current value and sends UIControlEventValueChanged when modified.
@IBInspectable public var value: Double = 0.0
/// Minimum value that must be less than the maximum value.
@IBInspectable public var minimumValue: Double = 0.0
/// Maximum value that must be greater than the minimum value.
@IBInspectable public var maximumValue: Double = 1.0
/// When set to true, the user can tap the label and manually enter a value.
@IBInspectable public var enableManualEditing: Bool = false
/// The value added/subtracted when one of the two buttons is pressed.
@IBInspectable public var stepValue: Double = 0.1
/// When set to true, keeping a button pressed will continuously increase/decrease the value every 0.1s.
@IBInspectable public var autorepeat: Bool = true
/// The background color of the stepper buttons while pressed.
@IBInspectable public var highlightedBackgroundColor: UIColor = UIColor(white: 1.0, alpha: 0.1)
/// The color of the +/- icons when in disabled state.
@IBInspectable public var disabledIconButtonColor: UIColor = UIColor.gray
/// The color of the +/- buttons background when in disabled state.
@IBInspectable public var disabledBackgroundButtonColor: UIColor = UIColor.clear
/// The background color of the plus and minus buttons.
@IBInspectable public var backgroundButtonColor: UIColor = UIColor.clear
/// The background color of the center view that contains the value label.
@IBInspectable public var backgroundLabelColor: UIColor = UIColor.clear
/// The text color of the value label in positioned in the center.
@IBInspectable public var labelTextColor: UIColor = UIColor.white
/// Describes the format of the value.
public var numberFormatter: NumberFormatter
// Default width of the stepper. Taken from the official UIStepper object.
public let defaultWidth = 141.0
// Default height of the stepper. Taken from the official UIStepper object.
public let defaultHeight = 29.0
/// Value label that displays the current value displayed at the center of the stepper.
public let valueLabel: UILabel
安装
ValueStepper 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
。
pod 'ValueStepper'
如果您更喜欢 Carthage,也可以使用。将此行添加到您的 Cartfile
。
github "BalestraPatrick/ValueStepper"
需求
iOS 8.3 和 Swift 4.0 是必需的。
- 如果您使用的是 Swift 4,请使用 master branch。
- 如果您使用的是 Swift 3.2,请使用 swift-3.2 branch。
- 如果您使用的是 Swift 2.3,请使用 swift-2.3 branch。
作者
我是Patrick Balestra。邮箱: [email protected] Twitter: @BalestraPatrick。
许可证
ValueStepper
遵循MIT许可证。更多信息请参阅LICENSE文件。