 
 import UIKit
 
-protocol SnappingSliderDelegate: class {
-    
+public protocol SnappingSliderDelegate: class {
     func snappingSliderDidIncrementValue(slider:SnappingSlider)
     func snappingSliderDidDecrementValue(slider:SnappingSlider)
 }
 
-class SnappingSlider: UIView {
+public class SnappingSlider: UIView {
 
     // Dem Exposed Variables
-    final weak var delegate:SnappingSliderDelegate?
-
-    final var incrementAndDecrementLabelFont:UIFont
-    final var incrementAndDecrementLabelTextColor:UIColor
-    final var incrementAndDecrementBackgroundColor:UIColor
-    
-    final var sliderColor:UIColor
-    final var sliderTitleFont:UIFont
-    final var sliderTitleColor:UIColor
-    final var sliderTitleText:String
-    
-    final var sliderCornerRadius:CGFloat
+    final public weak var delegate:SnappingSliderDelegate?
+
+    final public var incrementAndDecrementLabelFont:UIFont = UIFont(name: "TrebuchetMS-Bold", size: 18.0)! {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    final public var incrementAndDecrementLabelTextColor:UIColor = UIColor.whiteColor() {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    final public var incrementAndDecrementBackgroundColor:UIColor = UIColor(red:0.36, green:0.65, blue:0.65, alpha:1) {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    
+    final public var sliderColor:UIColor = UIColor(red:0.42, green:0.76, blue:0.74, alpha:1) {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    final public var sliderTitleFont:UIFont = UIFont(name: "TrebuchetMS-Bold", size: 15.0)! {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    final public var sliderTitleColor:UIColor = UIColor.whiteColor() {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    final public var sliderTitleText:String = "Slide Me" {
+        didSet {
+            setNeedsLayout()
+        }
+    }
+    
+    final public var sliderCornerRadius:CGFloat = 3.0 {
+        didSet {
+            setNeedsLayout()
+        }
+    }
     
     // Dem Private Variables
-    final private let sliderContainer:UIView = UIView(frame: CGRectZero)
-    final private let minusLabel:UILabel = UILabel(frame: CGRectZero)
-    final private let plusLabel:UILabel = UILabel(frame: CGRectZero)
-    final private let sliderView:UIView = UIView(frame: CGRectZero)
-    final private let sliderViewLabel:UILabel = UILabel(frame: CGRectZero)
-    
-    final private var isCurrentDraggingSlider:Bool = false
-    final private var lastDelegateFireOffset:CGFloat = 0
-    final private var touchesBeganPoint:CGPoint = CGPointZero
-    
-    final private let sliderPanGestureRecogniser:UIPanGestureRecognizer = UIPanGestureRecognizer()
-    final private let dynamicButtonAnimator:UIDynamicAnimator = UIDynamicAnimator()
+    final private let sliderContainer = UIView(frame: CGRectZero)
+    final private let minusLabel = UILabel(frame: CGRectZero)
+    final private let plusLabel = UILabel(frame: CGRectZero)
+    final private let sliderView = UIView(frame: CGRectZero)
+    final private let sliderViewLabel = UILabel(frame: CGRectZero)
+    
+    final private var isCurrentDraggingSlider = false
+    final private var lastDelegateFireOffset = CGFloat(0)
+    final private var touchesBeganPoint = CGPointZero
+    
+    final private let sliderPanGestureRecogniser = UIPanGestureRecognizer()
+    final private let dynamicButtonAnimator = UIDynamicAnimator()
     final private var snappingBehavior:SliderSnappingBehavior?
-    
-    final private let defaultIncrementAndDecrementLabelsBackgroundColor:UIColor = UIColor(red:0.36, green:0.65, blue:0.65, alpha:1)
-    final private let defaultIncrementAndDecrementLabelsFont:UIFont = UIFont(name: "TrebuchetMS-Bold", size: 18.0)!
-    final private let defaultIncrementAndDecrementLabelsTextColor:UIColor = UIColor.whiteColor()
-
-    final private let defaultSliderColor:UIColor = UIColor(red:0.42, green:0.76, blue:0.74, alpha:1)
-    final private let defaultSliderTitleFont:UIFont = UIFont(name: "TrebuchetMS-Bold", size: 15.0)!
-    final private let defaultSliderTitleColor:UIColor = UIColor.whiteColor()
-    final private let defaultsliderTitleText:String = "Slide Me"
-
-    final private let defaultCornerRadius:CGFloat = 3.0
-    
-    
+
+
     // MARK: Init & View Lifecycle
 
-    init(frame:CGRect, title:String) {
-        
-        incrementAndDecrementLabelFont = defaultIncrementAndDecrementLabelsFont
-        incrementAndDecrementBackgroundColor = defaultIncrementAndDecrementLabelsBackgroundColor
-        incrementAndDecrementLabelTextColor = defaultIncrementAndDecrementLabelsTextColor
-        
-        sliderColor = defaultSliderColor
-        sliderTitleFont = defaultSliderTitleFont
-        sliderTitleColor = defaultSliderTitleColor
+    public init(frame:CGRect, title:String) {
+
+        super.init(frame: frame)
+
         sliderTitleText = title
-        
-        sliderCornerRadius = defaultCornerRadius
-        
-        super.init(frame: frame)
-        
-        self.backgroundColor = backgroundColor
-        sliderContainer.backgroundColor = backgroundColor
-        
-        minusLabel.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.25, frame.size.height)
-        minusLabel.center = CGPointMake(minusLabel.bounds.size.width * 0.5, frame.size.height * 0.5)
-        minusLabel.text = "-"
+
+        setup()
+
+        setNeedsLayout()
+    }
+    
+    required public init(coder aDecoder: NSCoder) {
+
+        super.init(coder: aDecoder)
+
+        setup()
+
+        setNeedsLayout()
+    }
+    
+    override public func layoutSubviews() {
+        
+        super.layoutSubviews()
+        
+        if snappingBehavior?.snappingPoint.x != center.x {
+        
+            snappingBehavior = SliderSnappingBehavior(item: sliderView, snapToPoint: CGPointMake(bounds.size.width * 0.5, bounds.size.height * 0.5))
+            lastDelegateFireOffset = sliderView.center.x
+        }
+        
+        sliderContainer.frame = frame
+        sliderContainer.center = CGPointMake(bounds.size.width * 0.5, bounds.size.height * 0.5)
+        sliderContainer.backgroundColor = incrementAndDecrementBackgroundColor
+
+        minusLabel.frame = CGRectMake(0.0, 0.0, bounds.size.width * 0.25, bounds.size.height)
+        minusLabel.center = CGPointMake(minusLabel.bounds.size.width * 0.5, bounds.size.height * 0.5)
+        minusLabel.backgroundColor = incrementAndDecrementBackgroundColor
+        minusLabel.font = incrementAndDecrementLabelFont
         minusLabel.textColor = incrementAndDecrementLabelTextColor
-        minusLabel.font = incrementAndDecrementLabelFont
-        minusLabel.textAlignment = NSTextAlignment.Center
-        sliderContainer.addSubview(minusLabel)
-        
-        plusLabel.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.25, frame.size.height)
-        plusLabel.center = CGPointMake(frame.size.width - plusLabel.bounds.size.width * 0.5, frame.size.height * 0.5)
-        plusLabel.text = "+"
+        
+        plusLabel.frame = CGRectMake(0.0, 0.0, bounds.size.width * 0.25, bounds.size.height)
+        plusLabel.center = CGPointMake(bounds.size.width - plusLabel.bounds.size.width * 0.5, bounds.size.height * 0.5)
+        plusLabel.backgroundColor = incrementAndDecrementBackgroundColor
+        plusLabel.font = incrementAndDecrementLabelFont
         plusLabel.textColor = incrementAndDecrementLabelTextColor
-        plusLabel.font = incrementAndDecrementLabelFont
-        plusLabel.textAlignment = NSTextAlignment.Center
-        sliderContainer.addSubview(plusLabel)
-        
-        sliderView.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.5, frame.size.height)
-        sliderView.center = CGPointMake(frame.size.width * 0.5, frame.size.height * 0.5)
-        sliderContainer.addSubview(sliderView)
+        
+        sliderView.frame = CGRectMake(0.0, 0.0, bounds.size.width * 0.5, bounds.size.height)
+        sliderView.center = CGPointMake(bounds.size.width * 0.5, bounds.size.height * 0.5)
+        sliderView.backgroundColor = sliderColor
         
         sliderViewLabel.frame = CGRectMake(0.0, 0.0, sliderView.bounds.size.width, sliderView.bounds.size.height)
         sliderViewLabel.center = CGPointMake(sliderViewLabel.bounds.size.width * 0.5, sliderViewLabel.bounds.size.height * 0.5)
-        sliderViewLabel.userInteractionEnabled = false
         sliderViewLabel.backgroundColor = sliderColor
         sliderViewLabel.font = sliderTitleFont
         sliderViewLabel.text = sliderTitleText
-        sliderViewLabel.textAlignment = NSTextAlignment.Center
-        sliderViewLabel.textColor = sliderTitleColor
-        sliderView.addSubview(sliderViewLabel)
-        
-        sliderPanGestureRecogniser.addTarget(self, action: NSSelectorFromString("handleGesture:"))
-        sliderView.addGestureRecognizer(sliderPanGestureRecogniser)
-
-        sliderContainer.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        self.addSubview(sliderContainer)
-        self.clipsToBounds = true
-    }
-    
-    required init(coder aDecoder: NSCoder) {
-
-        incrementAndDecrementLabelFont = defaultIncrementAndDecrementLabelsFont
-        incrementAndDecrementBackgroundColor = defaultIncrementAndDecrementLabelsBackgroundColor
-        incrementAndDecrementLabelTextColor = UIColor.darkTextColor()
-        
-        sliderColor = defaultSliderColor
-        sliderTitleFont = defaultSliderTitleFont
-        sliderTitleColor = defaultSliderTitleColor
-        sliderTitleText = defaultsliderTitleText
-        
-        sliderCornerRadius = defaultCornerRadius
-
-        super.init(coder: aDecoder)
-        
-        self.backgroundColor = backgroundColor
-        sliderContainer.backgroundColor = backgroundColor
-        
-        minusLabel.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.25, frame.size.height)
-        minusLabel.center = CGPointMake(minusLabel.bounds.size.width * 0.5, frame.size.height * 0.5)
-        minusLabel.text = "-"
-        minusLabel.textColor = incrementAndDecrementLabelTextColor
-        minusLabel.font = incrementAndDecrementLabelFont
-        minusLabel.textAlignment = NSTextAlignment.Center
-        sliderContainer.addSubview(minusLabel)
-        
-        plusLabel.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.25, frame.size.height)
-        plusLabel.center = CGPointMake(frame.size.width - plusLabel.bounds.size.width * 0.5, frame.size.height * 0.5)
-        plusLabel.text = "+"
-        plusLabel.textColor = incrementAndDecrementLabelTextColor
-        plusLabel.font = incrementAndDecrementLabelFont
-        plusLabel.textAlignment = NSTextAlignment.Center
-        sliderContainer.addSubview(plusLabel)
-        
-        sliderView.frame = CGRectMake(0.0, 0.0, frame.size.width * 0.5, frame.size.height)
-        sliderView.center = CGPointMake(frame.size.width * 0.5, frame.size.height * 0.5)
-        sliderContainer.addSubview(sliderView)
-        
-        sliderViewLabel.frame = CGRectMake(0.0, 0.0, sliderView.bounds.size.width, sliderView.bounds.size.height)
-        sliderViewLabel.center = CGPointMake(sliderViewLabel.bounds.size.width * 0.5, sliderViewLabel.bounds.size.height * 0.5)
-        sliderViewLabel.userInteractionEnabled = false
-        sliderViewLabel.backgroundColor = sliderColor
-        sliderViewLabel.font = sliderTitleFont
-        sliderViewLabel.text = sliderTitleText
-        sliderViewLabel.textAlignment = NSTextAlignment.Center
-        sliderViewLabel.textColor = sliderTitleColor
-        sliderView.addSubview(sliderViewLabel)
-        
-        sliderPanGestureRecogniser.addTarget(self, action: NSSelectorFromString("handleGesture:"))
-        sliderView.addGestureRecognizer(sliderPanGestureRecogniser)
-        
-        sliderContainer.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        self.addSubview(sliderContainer)
-        self.clipsToBounds = true
-    }
-    
-    override func layoutSubviews() {
-        
-        super.layoutSubviews()
-        
-        if snappingBehavior?.snappingPoint.x != self.center.x {
-        
-            snappingBehavior = SliderSnappingBehavior(item: sliderView, snapToPoint: CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5))
-            lastDelegateFireOffset = sliderView.center.x
-        }
-        
-        sliderContainer.frame = self.frame
-        sliderContainer.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        sliderContainer.backgroundColor = incrementAndDecrementBackgroundColor
-        
-        minusLabel.frame = CGRectMake(0.0, 0.0, self.bounds.size.width * 0.25, self.bounds.size.height)
-        minusLabel.center = CGPointMake(minusLabel.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        minusLabel.backgroundColor = incrementAndDecrementBackgroundColor
-        minusLabel.font = incrementAndDecrementLabelFont
-        minusLabel.textColor = incrementAndDecrementLabelTextColor
-        
-        plusLabel.frame = CGRectMake(0.0, 0.0, self.bounds.size.width * 0.25, self.bounds.size.height)
-        plusLabel.center = CGPointMake(self.bounds.size.width - plusLabel.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        plusLabel.backgroundColor = incrementAndDecrementBackgroundColor
-        plusLabel.font = incrementAndDecrementLabelFont
-        plusLabel.textColor = incrementAndDecrementLabelTextColor
-        
-        sliderView.frame = CGRectMake(0.0, 0.0, self.bounds.size.width * 0.5, self.bounds.size.height)
-        sliderView.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5)
-        sliderView.backgroundColor = sliderColor
-        
-        sliderViewLabel.frame = CGRectMake(0.0, 0.0, sliderView.bounds.size.width, sliderView.bounds.size.height)
-        sliderViewLabel.center = CGPointMake(sliderViewLabel.bounds.size.width * 0.5, sliderViewLabel.bounds.size.height * 0.5)
-        sliderViewLabel.backgroundColor = sliderColor
-        sliderViewLabel.font = defaultSliderTitleFont
-        sliderViewLabel.text = sliderTitleText
-        
-        self.layer.cornerRadius = sliderCornerRadius
+        
+        layer.cornerRadius = sliderCornerRadius
     }
     
     // MARK: Gesture Handling

        
        super.init()
        
        self.addChildBehavior(dynamicItemBehavior)
        self.addChildBehavior(snapBehavior)
    }
}
                    
                    if (fabs(lastDelegateFireOffset - translatedCenterX) >= (sliderView.bounds.size.width * 0.15)) {
                        
                        delegate?.snappingSliderDidIncrementValue(self)
                        lastDelegateFireOffset = translatedCenterX
                    }
                }
                
            case .Ended:

                fallthrough
                
            case .Failed:

                fallthrough

            case .Cancelled:
                
                dynamicButtonAnimator.addBehavior(snappingBehavior)
                isCurrentDraggingSlider = false
                lastDelegateFireOffset = center.x

            case .Possible:

                // Swift requires at least one statement per case
                let x = 0
            }
        }
        
    }

    private func setup() {
        sliderContainer.backgroundColor = backgroundColor

        minusLabel.text = "-"
        minusLabel.textAlignment = NSTextAlignment.Center
        sliderContainer.addSubview(minusLabel)

        plusLabel.text = "+"
        plusLabel.textAlignment = NSTextAlignment.Center
        sliderContainer.addSubview(plusLabel)

        sliderContainer.addSubview(sliderView)

        sliderViewLabel.userInteractionEnabled = false
        sliderViewLabel.textAlignment = NSTextAlignment.Center
        sliderViewLabel.textColor = sliderTitleColor
        sliderView.addSubview(sliderViewLabel)

        sliderPanGestureRecogniser.addTarget(self, action: NSSelectorFromString("handleGesture:"))
        sliderView.addGestureRecognizer(sliderPanGestureRecogniser)

        sliderContainer.center = CGPointMake(bounds.size.width * 0.5, bounds.size.height * 0.5)
        addSubview(sliderContainer)
        clipsToBounds = true
    }
}

final class SliderSnappingBehavior: UIDynamicBehavior {
