Compare commits

...

10 Commits
dev ... master

Author SHA1 Message Date
Даша d7c8bb83b0
Merge pull request #2 from TouchInstinct/block_minimum_reached
Added block call when minimum is reached
2019-03-11 16:14:38 +03:00
DashaGitHub 810fcbefea Some refactoring 2019-03-11 16:10:21 +03:00
DashaGitHub f893371f73 Some refactoring 2019-03-11 16:05:15 +03:00
DashaGitHub 9444b32028 Some refactoring 2019-03-11 14:42:45 +03:00
DashaGitHub dd09a1e0d9 Added block call when minimum is reached. Refactoring 2019-03-11 14:37:33 +03:00
DashaGitHub a02560f7e0 Added block call when minimum is reached 2019-03-11 11:57:02 +03:00
Ivan Zinovyev 3c81fa9ade
Merge pull request #1 from iznv/stepper_customization
Stepper customisation
2018-11-01 23:15:18 +06:00
Ivan Zinovyev 719830b81e Update version in podspec 2018-11-01 19:53:14 +03:00
Ivan Zinovyev 82453c0681 Replace buttons text with images 2018-11-01 18:44:46 +03:00
Ivan Zinovyev 7367554d53 Add leftButtonLimitOpacity, buttonsTextInsets, didTouchLabel 2018-10-31 20:19:59 +03:00
2 changed files with 46 additions and 29 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" s.version = "2.2.1"
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,6 +15,8 @@ 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 {
@ -35,7 +37,6 @@ 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 {
@ -67,29 +68,37 @@ import UIKit
} }
} }
/// Text on the left button. Be sure that it fits in the button. Defaults to "". /// Image on the left button. Defaults to nil.
@objc @IBInspectable public var leftButtonText: String = "" { @objc @IBInspectable public var leftButtonImage: UIImage? = nil {
didSet { didSet {
leftButton.setTitle(leftButtonText, for: .normal) leftButton.setImage(leftButtonImage, for: .normal)
} }
} }
/// Text on the right button. Be sure that it fits in the button. Defaults to "+". /// Image on the right button. Defaults to nil.
@objc @IBInspectable public var rightButtonText: String = "+" { @objc @IBInspectable public var rightButtonImage: UIImage? = nil {
didSet { didSet {
rightButton.setTitle(rightButtonText, for: .normal) rightButton.setImage(rightButtonImage, for: .normal)
} }
} }
/// Text color of the buttons. Defaults to white. /// Left button content insets. Defaults to ".zero".
@objc @IBInspectable public var buttonsTextColor: UIColor = UIColor.white { @objc @IBInspectable public var leftButtonContentInsets: UIEdgeInsets = .zero {
didSet { didSet {
for button in [leftButton, rightButton] { leftButton.contentEdgeInsets = leftButtonContentInsets
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 {
@ -100,15 +109,12 @@ import UIKit
} }
} }
/// Font of the buttons. Defaults to AvenirNext-Bold, 20.0 points in size. /// Label tap closure
@objc public var buttonsFont = UIFont(name: "AvenirNext-Bold", size: 20.0)! { @objc public var didTouchLabel: ((Double) -> Void)?
didSet {
for button in [leftButton, rightButton] { /// Block is called when the minimum is exceeded
button.titleLabel?.font = buttonsFont @objc public var minimumExceeded: (() -> Void)?
}
}
}
/// 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 {
@ -188,10 +194,8 @@ import UIKit
lazy var leftButton: UIButton = { lazy var leftButton: UIButton = {
let button = UIButton() let button = UIButton()
button.setTitle(self.leftButtonText, for: .normal) button.setImage(self.leftButtonImage, 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)
@ -201,10 +205,8 @@ import UIKit
lazy var rightButton: UIButton = { lazy var rightButton: UIButton = {
let button = UIButton() let button = UIButton()
button.setTitle(self.rightButtonText, for: .normal) button.setImage(self.rightButtonImage, 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)
@ -225,6 +227,8 @@ 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
}() }()
@ -296,6 +300,7 @@ 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
@ -407,6 +412,10 @@ extension GMStepper {
} }
} }
@objc func handleLabelTap() {
didTouchLabel?(value)
}
@objc func reset() { @objc func reset() {
panState = .Stable panState = .Stable
stepperState = .Stable stepperState = .Stable
@ -433,11 +442,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) {
@ -519,8 +528,16 @@ 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)
} }