@@ -11,33 +11,18 @@ import UIKit
11
11
// Dedign by Oleg Frolov
12
12
//https://dribbble.com/shots/2028065-Switcher-lll
13
13
14
- open class TKLiquidSwitch : TKBaseSwitch , TKViewScale {
14
+ open class TKLiquidSwitch : TKBaseSwitch {
15
15
16
16
17
17
fileprivate var bubbleLayer = CAShapeLayer ( )
18
18
fileprivate var lineLayer = CAShapeLayer ( )
19
19
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 ) ) {
21
21
didSet {
22
22
setUpView ( )
23
23
}
24
24
}
25
25
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
- }
41
26
42
27
override internal func setUpView( ) {
43
28
super. setUpView ( )
@@ -56,43 +41,36 @@ open class TKLiquidSwitch: TKBaseSwitch, TKViewScale {
56
41
57
42
}
58
43
59
- override public func changeValue( ) {
44
+ override internal func changeValue( ) {
60
45
super. changeValue ( )
61
- changeStateAnimate ( isOn, duration: self . animateDuration)
62
- }
63
-
64
-
65
- func changeStateAnimate( _ turnOn: Bool , duration: Double ) {
66
46
67
-
68
-
69
47
let bubbleTransformAnim = CAKeyframeAnimation ( keyPath: " transform " )
70
48
bubbleTransformAnim. values = [ NSValue ( caTransform3D: CATransform3DIdentity) ,
71
49
NSValue ( caTransform3D: CATransform3DMakeScale ( 1 , 0.8 , 1 ) ) ,
72
50
NSValue ( caTransform3D: CATransform3DMakeScale ( 0.8 , 1 , 1 ) ) ,
73
51
NSValue ( caTransform3D: CATransform3DIdentity) ]
74
52
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
76
54
77
55
let bubblePositionAnim = CABasicAnimation ( keyPath: " position " )
78
56
bubblePositionAnim. fromValue = NSValue ( cgPoint: bubblePosition ( !isOn) )
79
57
bubblePositionAnim. toValue = NSValue ( cgPoint: bubblePosition ( isOn) )
80
- bubblePositionAnim. duration = duration
58
+ bubblePositionAnim. duration = animateDuration
81
59
82
60
let bubbleGroupAnim = CAAnimationGroup ( )
83
61
bubbleGroupAnim. animations = [ bubbleTransformAnim, bubblePositionAnim]
84
62
bubbleGroupAnim. isRemovedOnCompletion = false
85
63
bubbleGroupAnim. fillMode = kCAFillModeForwards
86
- bubbleGroupAnim. duration = duration
64
+ bubbleGroupAnim. duration = animateDuration
65
+
87
66
88
-
89
67
bubbleLayer. add ( bubbleGroupAnim, forKey: " Bubble " )
90
68
91
69
let color = switchColor ( isOn) . cgColor
92
- UIView . animate ( withDuration: duration , animations: { ( ) -> Void in
70
+ UIView . animate ( withDuration: animateDuration , animations: { ( ) -> Void in
93
71
self . lineLayer. fillColor = color
94
72
self . bubbleLayer. fillColor = color
95
- } )
73
+ } )
96
74
}
97
75
}
98
76
@@ -130,7 +108,7 @@ extension TKLiquidSwitch{
130
108
// let cL = CGPoint(x: sR, y: lR)
131
109
let cC = CGPoint ( x: sR * 2 + lR, y: lR)
132
110
// let cR = CGPoint(x: sR * 3 + lR * 2, y: lR)
133
-
111
+
134
112
bubblePath. move ( to: l1)
135
113
bubblePath. addQuadCurve ( to: c1, controlPoint: o1)
136
114
bubblePath. addArc ( withCenter: cC, radius: lR, startAngle: - CGFloat. pi/ 2 , endAngle: CGFloat . pi*3/ 2 , clockwise: true )
@@ -145,7 +123,7 @@ extension TKLiquidSwitch{
145
123
return bubblePath. cgPath
146
124
}
147
125
148
- func switchColor( _ isON : Bool ) -> UIColor {
126
+ func switchColor( _ isOn : Bool ) -> UIColor {
149
127
if isOn{
150
128
return color. on
151
129
}
@@ -154,7 +132,7 @@ extension TKLiquidSwitch{
154
132
}
155
133
}
156
134
157
- func bubblePosition( _ isOn: Bool ) -> CGPoint {
135
+ func bubblePosition( _ isOn : Bool ) -> CGPoint {
158
136
let h = self . bounds. height
159
137
let w = self . bounds. width
160
138
0 commit comments