Compare commits

..

No commits in common. "master" and "dev" have entirely different histories.
master ... dev

2 changed files with 29 additions and 46 deletions

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "GMStepper" s.name = "GMStepper"
s.version = "2.2.1" s.version = "2.2"
s.summary = "A stepper with a sliding label in the middle." s.summary = "A stepper with a sliding label in the middle."
s.homepage = "https://github.com/gmertk/GMStepper" s.homepage = "https://github.com/gmertk/GMStepper"
s.screenshots = "https://raw.githubusercontent.com/gmertk/GMStepper/master/Screenshots/screenshot_1.gif" s.screenshots = "https://raw.githubusercontent.com/gmertk/GMStepper/master/Screenshots/screenshot_1.gif"

View File

@ -15,8 +15,6 @@ import UIKit
didSet { didSet {
value = min(maximumValue, max(minimumValue, value)) value = min(maximumValue, max(minimumValue, value))
handleIsLimitReached()
label.text = formattedValue label.text = formattedValue
if oldValue != value { if oldValue != value {
@ -37,6 +35,7 @@ import UIKit
} }
} }
/// Minimum value. Must be less than maximumValue. Defaults to 0. /// Minimum value. Must be less than maximumValue. Defaults to 0.
@objc @IBInspectable public var minimumValue: Double = 0 { @objc @IBInspectable public var minimumValue: Double = 0 {
didSet { didSet {
@ -68,37 +67,29 @@ import UIKit
} }
} }
/// Image on the left button. Defaults to nil. /// Text on the left button. Be sure that it fits in the button. Defaults to "".
@objc @IBInspectable public var leftButtonImage: UIImage? = nil { @objc @IBInspectable public var leftButtonText: String = "" {
didSet { didSet {
leftButton.setImage(leftButtonImage, for: .normal) leftButton.setTitle(leftButtonText, for: .normal)
} }
} }
/// Image on the right button. Defaults to nil. /// Text on the right button. Be sure that it fits in the button. Defaults to "+".
@objc @IBInspectable public var rightButtonImage: UIImage? = nil { @objc @IBInspectable public var rightButtonText: String = "+" {
didSet { didSet {
rightButton.setImage(rightButtonImage, for: .normal) rightButton.setTitle(rightButtonText, for: .normal)
} }
} }
/// Left button content insets. Defaults to ".zero". /// Text color of the buttons. Defaults to white.
@objc @IBInspectable public var leftButtonContentInsets: UIEdgeInsets = .zero { @objc @IBInspectable public var buttonsTextColor: UIColor = UIColor.white {
didSet { didSet {
leftButton.contentEdgeInsets = leftButtonContentInsets for button in [leftButton, rightButton] {
button.setTitleColor(buttonsTextColor, for: .normal)
}
} }
} }
/// Right button content insets. Defaults to ".zero".
@objc @IBInspectable public var rightButtonContentInsets: UIEdgeInsets = .zero {
didSet {
rightButton.contentEdgeInsets = rightButtonContentInsets
}
}
/// Left button limit opacity. Defaults to "0.4".
@objc @IBInspectable public var leftButtonLimitOpacity: CGFloat = 0.4
/// Background color of the buttons. Defaults to dark blue. /// Background color of the buttons. Defaults to dark blue.
@objc @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) { @objc @IBInspectable public var buttonsBackgroundColor: UIColor = UIColor(red:0.21, green:0.5, blue:0.74, alpha:1) {
didSet { didSet {
@ -109,12 +100,15 @@ import UIKit
} }
} }
/// Label tap closure /// Font of the buttons. Defaults to AvenirNext-Bold, 20.0 points in size.
@objc public var didTouchLabel: ((Double) -> Void)? @objc public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! {
didSet {
/// Block is called when the minimum is exceeded for button in [leftButton, rightButton] {
@objc public var minimumExceeded: (() -> Void)? button.titleLabel?.font = buttonsFont
}
}
}
/// Text color of the middle label. Defaults to white. /// Text color of the middle label. Defaults to white.
@objc @IBInspectable public var labelTextColor: UIColor = UIColor.white { @objc @IBInspectable public var labelTextColor: UIColor = UIColor.white {
didSet { didSet {
@ -194,8 +188,10 @@ import UIKit
lazy var leftButton: UIButton = { lazy var leftButton: UIButton = {
let button = UIButton() let button = UIButton()
button.setImage(self.leftButtonImage, for: .normal) button.setTitle(self.leftButtonText, for: .normal)
button.setTitleColor(self.buttonsTextColor, for: .normal)
button.backgroundColor = self.buttonsBackgroundColor button.backgroundColor = self.buttonsBackgroundColor
button.titleLabel?.font = self.buttonsFont
button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), for: .touchDown) button.addTarget(self, action: #selector(GMStepper.leftButtonTouchDown), for: .touchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside) button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside) button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
@ -205,8 +201,10 @@ import UIKit
lazy var rightButton: UIButton = { lazy var rightButton: UIButton = {
let button = UIButton() let button = UIButton()
button.setImage(self.rightButtonImage, for: .normal) button.setTitle(self.rightButtonText, for: .normal)
button.setTitleColor(self.buttonsTextColor, for: .normal)
button.backgroundColor = self.buttonsBackgroundColor button.backgroundColor = self.buttonsBackgroundColor
button.titleLabel?.font = self.buttonsFont
button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), for: .touchDown) button.addTarget(self, action: #selector(GMStepper.rightButtonTouchDown), for: .touchDown)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside) button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpInside)
button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside) button.addTarget(self, action: #selector(GMStepper.buttonTouchUp), for: .touchUpOutside)
@ -227,8 +225,6 @@ import UIKit
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GMStepper.handlePan)) let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GMStepper.handlePan))
panRecognizer.maximumNumberOfTouches = 1 panRecognizer.maximumNumberOfTouches = 1
label.addGestureRecognizer(panRecognizer) label.addGestureRecognizer(panRecognizer)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(GMStepper.handleLabelTap))
label.addGestureRecognizer(tapRecognizer)
return label return label
}() }()
@ -300,7 +296,6 @@ import UIKit
addSubview(rightButton) addSubview(rightButton)
addSubview(label) addSubview(label)
handleIsLimitReached()
backgroundColor = buttonsBackgroundColor backgroundColor = buttonsBackgroundColor
layer.cornerRadius = cornerRadius layer.cornerRadius = cornerRadius
clipsToBounds = true clipsToBounds = true
@ -412,10 +407,6 @@ extension GMStepper {
} }
} }
@objc func handleLabelTap() {
didTouchLabel?(value)
}
@objc func reset() { @objc func reset() {
panState = .Stable panState = .Stable
stepperState = .Stable stepperState = .Stable
@ -442,11 +433,11 @@ extension GMStepper {
if value == minimumValue { if value == minimumValue {
animateLimitHitIfNeeded() animateLimitHitIfNeeded()
minimumExceeded?()
} else { } else {
stepperState = .ShouldDecrease stepperState = .ShouldDecrease
animateSlideLeft() animateSlideLeft()
} }
} }
@objc func rightButtonTouchDown(button: UIButton) { @objc func rightButtonTouchDown(button: UIButton) {
@ -528,16 +519,8 @@ extension GMStepper {
} }
} }
private extension GMStepper {
func handleIsLimitReached() {
let isLimitReached = value == minimumValue
leftButton.alpha = isLimitReached ? leftButtonLimitOpacity : 1
}
}
extension Decimal { extension Decimal {
var significantFractionalDecimalDigits: Int { var significantFractionalDecimalDigits: Int {
return max(-exponent, 0) return max(-exponent, 0)
} }