From c4bdefb5d0efe5c6feae1568a42dd23e65651e1b Mon Sep 17 00:00:00 2001 From: Gunay Mert Karadogan Date: Fri, 3 Jul 2015 16:24:31 +0200 Subject: [PATCH] Add sliding label. Closes #3 --- .../GMStepper/Base.lproj/Main.storyboard | 2 +- GMStepper/GMStepper/GMStepper.swift | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/GMStepper/GMStepper/Base.lproj/Main.storyboard b/GMStepper/GMStepper/Base.lproj/Main.storyboard index 7c78c6d..ccf2c1c 100644 --- a/GMStepper/GMStepper/Base.lproj/Main.storyboard +++ b/GMStepper/GMStepper/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + diff --git a/GMStepper/GMStepper/GMStepper.swift b/GMStepper/GMStepper/GMStepper.swift index 945867c..1c65fb2 100644 --- a/GMStepper/GMStepper/GMStepper.swift +++ b/GMStepper/GMStepper/GMStepper.swift @@ -93,6 +93,9 @@ import UIKit } } + private let labelSlideLength: CGFloat = 5 + private let labelSlideDuration: NSTimeInterval = 0.1 + private let leftButton = UIButton() private let rightButton = UIButton() private let label = UILabel() @@ -129,6 +132,7 @@ import UIKit label.font = labelFont addSubview(label) + backgroundColor = buttonsBackgroundColor layer.cornerRadius = cornerRadius clipsToBounds = true } @@ -144,9 +148,31 @@ import UIKit @objc private func leftButtonTapped(button: UIButton) { value -= 1 + animateSlideLeft() } @objc private func rightButtonTapped(button: UIButton) { value += 1 + animateSlideRight() + } + + private func animateSlideLeft() { + UIView.animateWithDuration(labelSlideDuration, animations: { + self.label.center.x -= self.labelSlideLength + }, completion : { _ in + UIView.animateWithDuration(self.labelSlideDuration, animations: { + self.label.center.x += self.labelSlideLength + }) + }) + } + + private func animateSlideRight() { + UIView.animateWithDuration(labelSlideDuration, animations: { + self.label.center.x += self.labelSlideLength + }, completion : { _ in + UIView.animateWithDuration(self.labelSlideDuration, animations: { + self.label.center.x -= self.labelSlideLength + }) + }) } }