Commit 2b1fa6e7 by Daniel Dahan

development: fixed merge conflicts

parents a239a68d 1523b5d4
...@@ -32,26 +32,26 @@ import UIKit ...@@ -32,26 +32,26 @@ import UIKit
@objc(SwitchStyle) @objc(SwitchStyle)
public enum SwitchStyle: Int { public enum SwitchStyle: Int {
case light case light
case dark case dark
} }
@objc(SwitchState) @objc(SwitchState)
public enum SwitchState: Int { public enum SwitchState: Int {
case on case on
case off case off
} }
@objc(SwitchSize) @objc(SwitchSize)
public enum SwitchSize: Int { public enum SwitchSize: Int {
case small case small
case medium case medium
case large case large
} }
@objc(SwitchDelegate) @objc(SwitchDelegate)
public protocol SwitchDelegate { public protocol SwitchDelegate {
/** /**
A Switch delegate method for state changes. A Switch delegate method for state changes.
- Parameter control: Switch control. - Parameter control: Switch control.
- Parameter state: SwitchState value. - Parameter state: SwitchState value.
...@@ -66,185 +66,185 @@ open class Switch: UIControl { ...@@ -66,185 +66,185 @@ open class Switch: UIControl {
} }
/// An internal reference to the switchState public property. /// An internal reference to the switchState public property.
fileprivate var internalSwitchState = SwitchState.off fileprivate var internalSwitchState = SwitchState.off
/// Track thickness. /// Track thickness.
fileprivate var trackThickness: CGFloat = 0 fileprivate var trackThickness: CGFloat = 0
/// Button diameter. /// Button diameter.
fileprivate var buttonDiameter: CGFloat = 0 fileprivate var buttonDiameter: CGFloat = 0
/// Position when in the .on state. /// Position when in the .on state.
fileprivate var onPosition: CGFloat = 0 fileprivate var onPosition: CGFloat = 0
/// Position when in the .off state. /// Position when in the .off state.
fileprivate var offPosition: CGFloat = 0 fileprivate var offPosition: CGFloat = 0
/// The bounce offset when animating. /// The bounce offset when animating.
fileprivate var bounceOffset: CGFloat = 3 fileprivate var bounceOffset: CGFloat = 3
/// An Optional delegation method. /// An Optional delegation method.
open weak var delegate: SwitchDelegate? open weak var delegate: SwitchDelegate?
/// Indicates if the animation should bounce. /// Indicates if the animation should bounce.
@IBInspectable @IBInspectable
open var isBounceable = true { open var isBounceable = true {
didSet { didSet {
bounceOffset = isBounceable ? 3 : 0 bounceOffset = isBounceable ? 3 : 0
} }
} }
/// Button on color. /// Button on color.
@IBInspectable @IBInspectable
open var buttonOnColor = Color.clear { open var buttonOnColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Button off color. /// Button off color.
@IBInspectable @IBInspectable
open var buttonOffColor = Color.clear { open var buttonOffColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Track on color. /// Track on color.
@IBInspectable @IBInspectable
open var trackOnColor = Color.clear { open var trackOnColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Track off color. /// Track off color.
@IBInspectable @IBInspectable
open var trackOffColor = Color.clear { open var trackOffColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Button on disabled color. /// Button on disabled color.
@IBInspectable @IBInspectable
open var buttonOnDisabledColor = Color.clear { open var buttonOnDisabledColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Track on disabled color. /// Track on disabled color.
@IBInspectable @IBInspectable
open var trackOnDisabledColor = Color.clear { open var trackOnDisabledColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Button off disabled color. /// Button off disabled color.
@IBInspectable @IBInspectable
open var buttonOffDisabledColor = Color.clear { open var buttonOffDisabledColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Track off disabled color. /// Track off disabled color.
@IBInspectable @IBInspectable
open var trackOffDisabledColor = Color.clear { open var trackOffDisabledColor = Color.clear {
didSet { didSet {
styleForState(state: switchState) styleForState(state: switchState)
} }
} }
/// Track view reference. /// Track view reference.
open fileprivate(set) var track: UIView { open fileprivate(set) var track: UIView {
didSet { didSet {
prepareTrack() prepareTrack()
} }
} }
/// Button view reference. /// Button view reference.
open fileprivate(set) var button: FABButton { open fileprivate(set) var button: FABButton {
didSet { didSet {
prepareButton() prepareButton()
} }
} }
@IBInspectable @IBInspectable
open override var isEnabled: Bool { open override var isEnabled: Bool {
didSet { didSet {
styleForState(state: internalSwitchState) styleForState(state: internalSwitchState)
} }
} }
/// A boolean indicating if the switch is on or not. /// A boolean indicating if the switch is on or not.
@IBInspectable @IBInspectable
public var isOn: Bool { public var isOn: Bool {
get { get {
return .on == internalSwitchState return .on == internalSwitchState
} }
set(value) { set(value) {
updateSwitchState(state: value ? .on : .off, animated: true, isTriggeredByUserInteraction: false) updateSwitchState(state: value ? .on : .off, animated: true, isTriggeredByUserInteraction: false)
} }
} }
/// Switch state. /// Switch state.
open var switchState: SwitchState { open var switchState: SwitchState {
get { get {
return internalSwitchState return internalSwitchState
} }
set(value) { set(value) {
updateSwitchState(state: value, animated: true, isTriggeredByUserInteraction: false) updateSwitchState(state: value, animated: true, isTriggeredByUserInteraction: false)
} }
} }
/// Switch style. /// Switch style.
open var switchStyle = SwitchStyle.dark { open var switchStyle = SwitchStyle.dark {
didSet { didSet {
switch switchStyle { switch switchStyle {
case .light: case .light:
buttonOnColor = Color.blue.darken2 buttonOnColor = Color.blue.darken2
trackOnColor = Color.blue.lighten3 trackOnColor = Color.blue.lighten3
buttonOffColor = Color.blueGrey.lighten4 buttonOffColor = Color.blueGrey.lighten4
trackOffColor = Color.grey.lighten3 trackOffColor = Color.grey.lighten3
buttonOnDisabledColor = Color.grey.lighten2 buttonOnDisabledColor = Color.grey.lighten2
trackOnDisabledColor = Color.grey.lighten3 trackOnDisabledColor = Color.grey.lighten3
buttonOffDisabledColor = Color.grey.lighten2 buttonOffDisabledColor = Color.grey.lighten2
trackOffDisabledColor = Color.grey.lighten3 trackOffDisabledColor = Color.grey.lighten3
case .dark: case .dark:
buttonOnColor = Color.blue.lighten1 buttonOnColor = Color.blue.lighten1
trackOnColor = Color.blue.lighten2.withAlphaComponent(0.5) trackOnColor = Color.blue.lighten2.withAlphaComponent(0.5)
buttonOffColor = Color.grey.lighten3 buttonOffColor = Color.grey.lighten3
trackOffColor = Color.blueGrey.lighten4.withAlphaComponent(0.5) trackOffColor = Color.blueGrey.lighten4.withAlphaComponent(0.5)
buttonOnDisabledColor = Color.grey.darken3 buttonOnDisabledColor = Color.grey.darken3
trackOnDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2) trackOnDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2)
buttonOffDisabledColor = Color.grey.darken3 buttonOffDisabledColor = Color.grey.darken3
trackOffDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2) trackOffDisabledColor = Color.grey.lighten1.withAlphaComponent(0.2)
} }
} }
} }
/// Switch size. /// Switch size.
open var switchSize = SwitchSize.medium { open var switchSize = SwitchSize.medium {
didSet { didSet {
switch switchSize { switch switchSize {
case .small: case .small:
trackThickness = 12 trackThickness = 12
buttonDiameter = 18 buttonDiameter = 18
case .medium: case .medium:
trackThickness = 18 trackThickness = 18
buttonDiameter = 24 buttonDiameter = 24
case .large: case .large:
trackThickness = 24 trackThickness = 24
buttonDiameter = 32 buttonDiameter = 32
} }
frame.size = intrinsicContentSize frame.size = intrinsicContentSize
} }
} }
open override var intrinsicContentSize: CGSize { open override var intrinsicContentSize: CGSize {
switch switchSize { switch switchSize {
case .small: case .small:
...@@ -255,48 +255,48 @@ open class Switch: UIControl { ...@@ -255,48 +255,48 @@ open class Switch: UIControl {
return CGSize(width: 36, height: 36) return CGSize(width: 36, height: 36)
} }
} }
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
track = UIView() track = UIView()
button = FABButton() button = FABButton()
super.init(coder: aDecoder) super.init(coder: aDecoder)
prepare() prepare()
} }
/** /**
An initializer that initializes the object with a CGRect object. An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance If AutoLayout is used, it is better to initilize the instance
using the init(state:style:size:) initializer, or set the CGRect using the init(state:style:size:) initializer, or set the CGRect
to CGRectNull. to CGRectNull.
- Parameter frame: A CGRect instance. - Parameter frame: A CGRect instance.
*/ */
public override init(frame: CGRect) { public override init(frame: CGRect) {
track = UIView() track = UIView()
button = FABButton() button = FABButton()
super.init(frame: frame) super.init(frame: frame)
prepare() prepare()
} }
/** /**
An initializer that sets the state, style, and size of the Switch instance. An initializer that sets the state, style, and size of the Switch instance.
- Parameter state: A SwitchState value. - Parameter state: A SwitchState value.
- Parameter style: A SwitchStyle value. - Parameter style: A SwitchStyle value.
- Parameter size: A SwitchSize value. - Parameter size: A SwitchSize value.
*/ */
public init(state: SwitchState = .off, style: SwitchStyle = .dark, size: SwitchSize = .medium) { public init(state: SwitchState = .off, style: SwitchStyle = .dark, size: SwitchSize = .medium) {
track = UIView() track = UIView()
button = FABButton() button = FABButton()
super.init(frame: .zero) super.init(frame: .zero)
prepare() prepare()
prepareSwitchState(state: state) prepareSwitchState(state: state)
prepareSwitchStyle(style: style) prepareSwitchStyle(style: style)
prepareSwitchSize(size: size) prepareSwitchSize(size: size)
} }
open override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
guard willLayout else { guard willLayout else {
...@@ -323,18 +323,18 @@ open class Switch: UIControl { ...@@ -323,18 +323,18 @@ open class Switch: UIControl {
} }
} }
open override func willMove(toSuperview newSuperview: UIView?) { open override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview) super.willMove(toSuperview: newSuperview)
styleForState(state: internalSwitchState) styleForState(state: internalSwitchState)
} }
/** /**
Toggle the Switch state, if On will be Off, and if Off will be On. Toggle the Switch state, if On will be Off, and if Off will be On.
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
open func toggle(completion: ((Switch) -> Void)? = nil) { open func toggle(completion: ((Switch) -> Void)? = nil) {
updateSwitchState(state: .on == internalSwitchState ? .off : .on, animated: true, isTriggeredByUserInteraction: false, completion: completion) updateSwitchState(state: .on == internalSwitchState ? .off : .on, animated: true, isTriggeredByUserInteraction: false, completion: completion)
} }
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard track.frame.contains(layer.convert(touches.first!.location(in: self), from: layer)) else { guard track.frame.contains(layer.convert(touches.first!.location(in: self), from: layer)) else {
...@@ -467,23 +467,23 @@ extension Switch { ...@@ -467,23 +467,23 @@ extension Switch {
fileprivate func animateToState(state: SwitchState, completion: ((Switch) -> Void)? = nil) { fileprivate func animateToState(state: SwitchState, completion: ((Switch) -> Void)? = nil) {
isUserInteractionEnabled = false isUserInteractionEnabled = false
UIView.animate(withDuration: 0.15, UIView.animate(withDuration: 0.15,
delay: 0.05, delay: 0.05,
options: [.curveEaseIn, .curveEaseOut], options: [.curveEaseIn, .curveEaseOut],
animations: { [weak self] in animations: { [weak self] in
guard let s = self else { guard let s = self else {
return return
} }
s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset
s.styleForState(state: state) s.styleForState(state: state)
}) { [weak self] _ in }) { [weak self] _ in
UIView.animate(withDuration: 0.15, UIView.animate(withDuration: 0.15,
animations: { [weak self] in animations: { [weak self] in
guard let s = self else { guard let s = self else {
return return
} }
s.button.x = .on == state ? s.onPosition : s.offPosition s.button.x = .on == state ? s.onPosition : s.offPosition
}) { [weak self] _ in }) { [weak self] _ in
guard let s = self else { guard let s = self else {
return return
......
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