Commit 531f2711 by Daniel Dahan

updated MaterialSwitch API to use 'highlighted', 'selected', 'on', 'setOn',…

updated MaterialSwitch API to use 'highlighted', 'selected', 'on', 'setOn', 'state', and public 'switchState' mutators
parent 13691a94
...@@ -111,8 +111,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate { ...@@ -111,8 +111,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
MaterialLayout.alignToParentVertically(bottomView, children: [c1, c2, c3]) MaterialLayout.alignToParentVertically(bottomView, children: [c1, c2, c3])
} }
internal func materialSwitchStateChanged(control: MaterialSwitch, state: MaterialSwitchState) { internal func materialSwitchStateChanged(control: MaterialSwitch) {
print("MaterialSwitch - Size: \(control.switchSize) State: \(state)") print("MaterialSwitch - Style: \(control.switchStyle), Size: \(control.switchSize), State: \(control.switchState), On: \(control.on), Selected: \(control.selected), Highlighted: \(control.highlighted)")
} }
} }
...@@ -52,10 +52,13 @@ public protocol MaterialSwitchDelegate { ...@@ -52,10 +52,13 @@ public protocol MaterialSwitchDelegate {
- Parameter control: MaterialSwitch control. - Parameter control: MaterialSwitch control.
- Parameter state: The new state for the control. - Parameter state: The new state for the control.
*/ */
func materialSwitchStateChanged(control: MaterialSwitch, state: MaterialSwitchState) func materialSwitchStateChanged(control: MaterialSwitch)
} }
public class MaterialSwitch: UIControl { public class MaterialSwitch: UIControl {
/// An internal reference to the switchState public property.
private var internalSwitchState: MaterialSwitchState = .Off
/// Track thickness. /// Track thickness.
private var trackThickness: CGFloat = 0 private var trackThickness: CGFloat = 0
...@@ -71,12 +74,6 @@ public class MaterialSwitch: UIControl { ...@@ -71,12 +74,6 @@ public class MaterialSwitch: UIControl {
/// The bounce offset when animating. /// The bounce offset when animating.
private var bounceOffset: CGFloat = 3 private var bounceOffset: CGFloat = 3
public override var enabled: Bool {
didSet {
styleForState(switchState)
}
}
/// A property that accesses the layer.frame.origin.x property. /// A property that accesses the layer.frame.origin.x property.
public var x: CGFloat { public var x: CGFloat {
get { get {
...@@ -165,10 +162,56 @@ public class MaterialSwitch: UIControl { ...@@ -165,10 +162,56 @@ public class MaterialSwitch: UIControl {
} }
} }
/// MaterialSwitch state. public override var enabled: Bool {
public var switchState: MaterialSwitchState = .Off {
didSet { didSet {
setSwitchState(switchState, animated: true) styleForState(internalSwitchState)
}
}
public override var selected: Bool {
get {
return .On == internalSwitchState
}
set(value) {
setOn(selected, animated: true)
}
}
public override var highlighted: Bool {
get {
return .On == internalSwitchState
}
set(value) {
setOn(highlighted, animated: true)
}
}
public override var state: UIControlState {
if enabled {
return selected ? [.Selected, .Highlighted] : [.Normal]
}
return selected ? [.Selected, .Highlighted, .Disabled] : [.Normal, .Disabled]
}
/// MaterialSwitch state.
public var switchState: MaterialSwitchState {
get {
return internalSwitchState
}
set(value) {
if value != internalSwitchState {
internalSwitchState = value
}
}
}
/// A boolean indicating if the switch is on or not.
public var on: Bool {
get {
return .On == internalSwitchState
}
set(value) {
setOn(on, animated: true)
} }
} }
...@@ -238,6 +281,7 @@ public class MaterialSwitch: UIControl { ...@@ -238,6 +281,7 @@ public class MaterialSwitch: UIControl {
trackLayer = MaterialLayer(frame: CGRectZero) trackLayer = MaterialLayer(frame: CGRectZero)
button = FabButton(frame: CGRectZero) button = FabButton(frame: CGRectZero)
super.init(coder: aDecoder) super.init(coder: aDecoder)
prepareView()
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchSize(.Default) prepareSwitchSize(.Default)
...@@ -255,6 +299,7 @@ public class MaterialSwitch: UIControl { ...@@ -255,6 +299,7 @@ public class MaterialSwitch: UIControl {
trackLayer = MaterialLayer(frame: CGRectZero) trackLayer = MaterialLayer(frame: CGRectZero)
button = FabButton(frame: CGRectZero) button = FabButton(frame: CGRectZero)
super.init(frame: CGRectZero) super.init(frame: CGRectZero)
prepareView()
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchSize(size) prepareSwitchSize(size)
...@@ -264,7 +309,7 @@ public class MaterialSwitch: UIControl { ...@@ -264,7 +309,7 @@ public class MaterialSwitch: UIControl {
public override func willMoveToSuperview(newSuperview: UIView?) { public override func willMoveToSuperview(newSuperview: UIView?) {
super.willMoveToSuperview(newSuperview) super.willMoveToSuperview(newSuperview)
styleForState(switchState) styleForState(internalSwitchState)
} }
public override func intrinsicContentSize() -> CGSize { public override func intrinsicContentSize() -> CGSize {
...@@ -286,7 +331,7 @@ public class MaterialSwitch: UIControl { ...@@ -286,7 +331,7 @@ public class MaterialSwitch: UIControl {
when subclassing. when subclassing.
*/ */
public func prepareView() { public func prepareView() {
addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTapped")) addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTapped:"))
} }
/** /**
...@@ -294,7 +339,16 @@ public class MaterialSwitch: UIControl { ...@@ -294,7 +339,16 @@ public class MaterialSwitch: UIControl {
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
public func toggle(completion: ((control: MaterialSwitch) -> Void)? = nil) { public func toggle(completion: ((control: MaterialSwitch) -> Void)? = nil) {
setSwitchState(.On == switchState ? .Off : .On, animated: true, completion: completion) setSwitchState(.On == internalSwitchState ? .Off : .On, animated: true, completion: completion)
}
/**
Sets the switch on or off.
- Parameter on: A bool of whether the switch should be in the on state or not.
- Parameter animated: A Boolean indicating to set the animation or not.
*/
public func setOn(on: Bool, animated: Bool, completion: ((control: MaterialSwitch) -> Void)? = nil) {
setSwitchState(on == true ? .On : .Off, animated: animated, completion: completion)
} }
/** /**
...@@ -304,39 +358,30 @@ public class MaterialSwitch: UIControl { ...@@ -304,39 +358,30 @@ public class MaterialSwitch: UIControl {
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
public func setSwitchState(state: MaterialSwitchState, animated: Bool = true, completion: ((control: MaterialSwitch) -> Void)? = nil) { public func setSwitchState(state: MaterialSwitchState, animated: Bool = true, completion: ((control: MaterialSwitch) -> Void)? = nil) {
if switchState != state { if internalSwitchState != state {
internalSwitchState = state
if animated { if animated {
userInteractionEnabled = false animateToState(state) { [unowned self] _ in
UIView.animateWithDuration(0.15,
delay: 0.05,
options: .CurveEaseInOut,
animations: { [unowned self] in
self.button.x = .On == state ? self.onPosition + self.bounceOffset : self.offPosition - self.bounceOffset
self.styleForState(state)
}) { [unowned self] _ in
self.styleForState(state)
self.sendActionsForControlEvents(.ValueChanged) self.sendActionsForControlEvents(.ValueChanged)
self.switchState = state self.delegate?.materialSwitchStateChanged(self)
self.delegate?.materialSwitchStateChanged(self, state: state)
UIView.animateWithDuration(0.15,
animations: { [unowned self] in
self.button.x = .On == state ? self.onPosition : self.offPosition
}) { [unowned self] _ in
self.userInteractionEnabled = true
completion?(control: self)
}
} }
} else { } else {
button.x = .On == state ? self.onPosition : self.offPosition button.x = .On == state ? self.onPosition : self.offPosition
styleForState(state) styleForState(state)
sendActionsForControlEvents(.ValueChanged) sendActionsForControlEvents(.ValueChanged)
switchState = state delegate?.materialSwitchStateChanged(self)
delegate?.materialSwitchStateChanged(self, state: state)
completion?(control: self) completion?(control: self)
} }
} }
} }
/// Handles the tap gesture.
internal func handleTapped(recognizer: UITapGestureRecognizer) {
if true == CGRectContainsPoint(trackLayer.frame, layer.convertPoint(recognizer.locationInView(self), fromLayer: layer)) {
setSwitchState(.On == internalSwitchState ? .Off : .On)
}
}
/// Handles the TouchUpInside event. /// Handles the TouchUpInside event.
internal func handleTouchUpInside() { internal func handleTouchUpInside() {
toggle() toggle()
...@@ -372,13 +417,6 @@ public class MaterialSwitch: UIControl { ...@@ -372,13 +417,6 @@ public class MaterialSwitch: UIControl {
} }
} }
public override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
if true == CGRectContainsPoint(trackLayer.frame, layer.convertPoint(touches.first!.locationInView(self), fromLayer: layer)) {
setSwitchState(.On == switchState ? .Off : .On)
}
}
/// Prepares the track. /// Prepares the track.
private func prepareTrack() { private func prepareTrack() {
layer.addSublayer(trackLayer) layer.addSublayer(trackLayer)
...@@ -482,8 +520,32 @@ public class MaterialSwitch: UIControl { ...@@ -482,8 +520,32 @@ public class MaterialSwitch: UIControl {
onPosition = width - px - buttonDiameter onPosition = width - px - buttonDiameter
offPosition = px offPosition = px
if .On == switchState { if .On == internalSwitchState {
button.x = onPosition button.x = onPosition
} }
} }
/**
Set the switchState property with an animate.
- Parameter state: The MaterialSwitchState to set.
- Parameter completion: An Optional completion block.
*/
private func animateToState(state: MaterialSwitchState, completion: ((control: MaterialSwitch) -> Void)? = nil) {
userInteractionEnabled = false
UIView.animateWithDuration(0.15,
delay: 0.05,
options: .CurveEaseInOut,
animations: { [unowned self] in
self.button.x = .On == state ? self.onPosition + self.bounceOffset : self.offPosition - self.bounceOffset
self.styleForState(state)
}) { [unowned self] _ in
UIView.animateWithDuration(0.15,
animations: { [unowned self] in
self.button.x = .On == state ? self.onPosition : self.offPosition
}) { [unowned self] _ in
self.userInteractionEnabled = true
completion?(control: self)
}
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment