Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limitHitBackgroundColor feature #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 33 additions & 4 deletions GMStepper/GMStepper.swift
Expand Up @@ -168,6 +168,13 @@ import UIKit
setNeedsLayout()
}
}

/// Color of the background of buttons in case the value hit the limit.
@objc @IBInspectable public var limitHitBackgroundColor: UIColor? {
didSet {
self.resetButtonsBackGroundColors()
}
}

/// Color of the flashing animation on the buttons in case the value hit the limit.
@objc @IBInspectable public var limitHitAnimationColor: UIColor = UIColor(red:0.26, green:0.6, blue:0.87, alpha:1)
Expand Down Expand Up @@ -300,6 +307,7 @@ import UIKit
layer.cornerRadius = cornerRadius
clipsToBounds = true
labelOriginalCenter = label.center
self.resetButtonsBackGroundColors()

setupNumberFormatter()

Expand Down Expand Up @@ -397,8 +405,7 @@ extension GMStepper {
stepperState = .Stable
resetTimer()

self.rightButton.backgroundColor = self.buttonsBackgroundColor
self.leftButton.backgroundColor = self.buttonsBackgroundColor
resetButtonsBackGroundColors()
}
case .ended, .cancelled, .failed:
reset()
Expand All @@ -418,10 +425,32 @@ extension GMStepper {

UIView.animate(withDuration: self.labelSlideDuration, animations: {
self.label.center = self.labelOriginalCenter
self.rightButton.backgroundColor = self.buttonsBackgroundColor
self.leftButton.backgroundColor = self.buttonsBackgroundColor
self.resetButtonsBackGroundColors()
})
}

private func resetButtonsBackGroundColors() {

func resetButtonsBackGroundColors(_ buttons:[UIButton]){
for button in buttons {
button.backgroundColor = buttonsBackgroundColor
}
}

if let disableStateColor = self.limitHitBackgroundColor {
if self.value == self.minimumValue {
self.leftButton.backgroundColor = self.limitHitBackgroundColor
resetButtonsBackGroundColors([rightButton])
} else if value == self.maximumValue{
self.rightButton.backgroundColor = self.limitHitBackgroundColor
resetButtonsBackGroundColors([leftButton])
} else {
resetButtonsBackGroundColors([leftButton, rightButton])
}
} else {
resetButtonsBackGroundColors([leftButton, rightButton])
}
}
}

// MARK: Button Events
Expand Down
1 change: 1 addition & 0 deletions GMStepperExample/GMStepperExample/ViewController.swift
Expand Up @@ -13,6 +13,7 @@ class ViewController: UIViewController {
@IBOutlet weak var stepper: GMStepper!
override func viewDidLoad() {
super.viewDidLoad()
// stepper.limitHitBackgroundColor = UIColor.purple
stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), for: .valueChanged)
}

Expand Down