@@ -11,33 +11,18 @@ import UIKit
1111// Dedign by Oleg Frolov
1212//https://dribbble.com/shots/2028065-Switcher-lll
1313
14- open class TKLiquidSwitch : TKBaseSwitch , TKViewScale {
14+ open class TKLiquidSwitch : TKBaseSwitch {
1515
1616
1717 fileprivate var bubbleLayer = CAShapeLayer ( )
1818 fileprivate var lineLayer = CAShapeLayer ( )
1919 fileprivate var color = ( on: UIColor ( red: 0.373 , green: 0.843 , blue: 0.596 , alpha: 1 ) ,
20- off: UIColor ( red: 0.871 , green: 0.871 , blue: 0.871 , alpha: 1 ) ) {
20+ off: UIColor ( red: 0.871 , green: 0.871 , blue: 0.871 , alpha: 1 ) ) {
2121 didSet {
2222 setUpView ( )
2323 }
2424 }
2525
26- override public init ( frame: CGRect ) {
27- super. init ( frame: frame)
28- setUpView ( )
29- }
30-
31- required public init ? ( coder aDecoder: NSCoder ) {
32- super. init ( coder: aDecoder)
33- setUpView ( )
34- }
35-
36-
37- open override func draw( _ rect: CGRect ) {
38- super. draw ( rect)
39-
40- }
4126
4227 override internal func setUpView( ) {
4328 super. setUpView ( )
@@ -56,43 +41,36 @@ open class TKLiquidSwitch: TKBaseSwitch, TKViewScale {
5641
5742 }
5843
59- override public func changeValue( ) {
44+ override internal func changeValue( ) {
6045 super. changeValue ( )
61- changeStateAnimate ( isOn, duration: self . animateDuration)
62- }
63-
64-
65- func changeStateAnimate( _ turnOn: Bool , duration: Double ) {
6646
67-
68-
6947 let bubbleTransformAnim = CAKeyframeAnimation ( keyPath: " transform " )
7048 bubbleTransformAnim. values = [ NSValue ( caTransform3D: CATransform3DIdentity) ,
7149 NSValue ( caTransform3D: CATransform3DMakeScale ( 1 , 0.8 , 1 ) ) ,
7250 NSValue ( caTransform3D: CATransform3DMakeScale ( 0.8 , 1 , 1 ) ) ,
7351 NSValue ( caTransform3D: CATransform3DIdentity) ]
7452 bubbleTransformAnim. keyTimes = [ NSNumber ( value: 0 ) , NSNumber ( value: 1.0 / 3.0 ) , NSNumber ( value: 2.0 / 3.0 ) , NSNumber ( value: 1 ) ]
75- bubbleTransformAnim. duration = duration
53+ bubbleTransformAnim. duration = animateDuration
7654
7755 let bubblePositionAnim = CABasicAnimation ( keyPath: " position " )
7856 bubblePositionAnim. fromValue = NSValue ( cgPoint: bubblePosition ( !isOn) )
7957 bubblePositionAnim. toValue = NSValue ( cgPoint: bubblePosition ( isOn) )
80- bubblePositionAnim. duration = duration
58+ bubblePositionAnim. duration = animateDuration
8159
8260 let bubbleGroupAnim = CAAnimationGroup ( )
8361 bubbleGroupAnim. animations = [ bubbleTransformAnim, bubblePositionAnim]
8462 bubbleGroupAnim. isRemovedOnCompletion = false
8563 bubbleGroupAnim. fillMode = kCAFillModeForwards
86- bubbleGroupAnim. duration = duration
64+ bubbleGroupAnim. duration = animateDuration
65+
8766
88-
8967 bubbleLayer. add ( bubbleGroupAnim, forKey: " Bubble " )
9068
9169 let color = switchColor ( isOn) . cgColor
92- UIView . animate ( withDuration: duration , animations: { ( ) -> Void in
70+ UIView . animate ( withDuration: animateDuration , animations: { ( ) -> Void in
9371 self . lineLayer. fillColor = color
9472 self . bubbleLayer. fillColor = color
95- } )
73+ } )
9674 }
9775}
9876
@@ -130,7 +108,7 @@ extension TKLiquidSwitch{
130108// let cL = CGPoint(x: sR, y: lR)
131109 let cC = CGPoint ( x: sR * 2 + lR, y: lR)
132110// let cR = CGPoint(x: sR * 3 + lR * 2, y: lR)
133-
111+
134112 bubblePath. move ( to: l1)
135113 bubblePath. addQuadCurve ( to: c1, controlPoint: o1)
136114 bubblePath. addArc ( withCenter: cC, radius: lR, startAngle: - CGFloat. pi/ 2 , endAngle: CGFloat . pi*3/ 2 , clockwise: true )
@@ -145,7 +123,7 @@ extension TKLiquidSwitch{
145123 return bubblePath. cgPath
146124 }
147125
148- func switchColor( _ isON : Bool ) -> UIColor {
126+ func switchColor( _ isOn : Bool ) -> UIColor {
149127 if isOn{
150128 return color. on
151129 }
@@ -154,7 +132,7 @@ extension TKLiquidSwitch{
154132 }
155133 }
156134
157- func bubblePosition( _ isOn: Bool ) -> CGPoint {
135+ func bubblePosition( _ isOn : Bool ) -> CGPoint {
158136 let h = self . bounds. height
159137 let w = self . bounds. width
160138
0 commit comments