@@ -11,7 +11,7 @@ import UIKit
1111// Dedign by Oleg Frolov
1212//https://dribbble.com/shots/2028065-Switcher-lll
1313
14- private class TKLiquidSwitch : TKBaseSwitch , TKViewScale {
14+ open class TKLiquidSwitch : TKBaseSwitch , TKViewScale {
1515
1616
1717 fileprivate var bubbleLayer = CAShapeLayer ( )
@@ -33,14 +33,19 @@ private class TKLiquidSwitch: TKBaseSwitch, TKViewScale {
3333 setUpView ( )
3434 }
3535
36+
37+ open override func draw( _ rect: CGRect ) {
38+ super. draw ( rect)
39+
40+ }
41+
3642 override internal func setUpView( ) {
3743 super. setUpView ( )
3844
3945 self . clipsToBounds = true
4046
41- lineLayer. path = UIBezierPath ( roundedRect: CGRect ( x: 0 , y: 30 , width: self . bounds. width, height: 20 * sizeScale) , cornerRadius: 10 * sizeScale) . cgPath
47+ lineLayer. path = UIBezierPath ( roundedRect: CGRect ( x: 0 , y: ( self . bounds . height - 20 * sizeScale ) / 2 , width: self . bounds. width, height: 20 * sizeScale) , cornerRadius: 10 * sizeScale) . cgPath
4248 lineLayer. fillColor = switchColor ( true ) . cgColor
43- lineLayer. position = CGPoint ( x: 0 , y: - 10 * sizeScale)
4449 self . layer. addSublayer ( lineLayer)
4550
4651 bubbleLayer. frame = self . bounds
@@ -94,27 +99,49 @@ private class TKLiquidSwitch: TKBaseSwitch, TKViewScale {
9499
95100// Getter
96101extension TKLiquidSwitch {
102+
103+ var bubbleSize : CGSize {
104+ let lineH = 20 * sizeScale
105+ let w = lineH * 2 + bounds. height
106+ let h = bounds. height
107+ return CGSize ( width: w, height: h)
108+ }
97109
98110 var bubbleShapePath : CGPath {
99111 let bubblePath = UIBezierPath ( )
100- let CGPointScaleMake = CGPointScaleMaker ( self . sizeScale)
101-
102- bubblePath. move ( to: CGPointScaleMake ( 120.6 , 34.75 ) )
103- bubblePath. addCurve ( to: CGPointScaleMake ( 150 , 50 ) , controlPoint1: CGPointScaleMake ( 125.45 , 43.71 ) , controlPoint2: CGPointScaleMake ( 136.79 , 50 ) )
104- bubblePath. addCurve ( to: CGPointScaleMake ( 138.26 , 51.75 ) , controlPoint1: CGPointScaleMake ( 145.85 , 50 ) , controlPoint2: CGPointScaleMake ( 141.89 , 50.62 ) )
105- bubblePath. addCurve ( to: CGPointScaleMake ( 120.6 , 65.25 ) , controlPoint1: CGPointScaleMake ( 130.31 , 54.21 ) , controlPoint2: CGPointScaleMake ( 123.93 , 59.1 ) )
106- bubblePath. addCurve ( to: CGPointScaleMake ( 75 , 100 ) , controlPoint1: CGPointScaleMake ( 114.43 , 85.41 ) , controlPoint2: CGPointScaleMake ( 96.35 , 100 ) )
107- bubblePath. addCurve ( to: CGPointScaleMake ( 29.4 , 65.25 ) , controlPoint1: CGPointScaleMake ( 53.65 , 100 ) , controlPoint2: CGPointScaleMake ( 35.57 , 85.41 ) )
108- bubblePath. addCurve ( to: CGPointScaleMake ( 16.25 , 53.48 ) , controlPoint1: CGPointScaleMake ( 26.73 , 60.31 ) , controlPoint2: CGPointScaleMake ( 22.09 , 56.19 ) )
109- bubblePath. addCurve ( to: CGPointScaleMake ( 11.74 , 51.75 ) , controlPoint1: CGPointScaleMake ( 14.82 , 52.81 ) , controlPoint2: CGPointScaleMake ( 13.31 , 52.23 ) )
110- bubblePath. addCurve ( to: CGPointScaleMake ( 0 , 50 ) , controlPoint1: CGPointScaleMake ( 8.11 , 50.62 ) , controlPoint2: CGPointScaleMake ( 4.15 , 50 ) )
111- bubblePath. addCurve ( to: CGPointScaleMake ( 11 , 48.48 ) , controlPoint1: CGPointScaleMake ( 3.87 , 50 ) , controlPoint2: CGPointScaleMake ( 7.57 , 49.46 ) )
112- bubblePath. addCurve ( to: CGPointScaleMake ( 29.4 , 34.75 ) , controlPoint1: CGPointScaleMake ( 19.29 , 46.09 ) , controlPoint2: CGPointScaleMake ( 25.97 , 41.09 ) )
113- bubblePath. addCurve ( to: CGPointScaleMake ( 38.05 , 18.2 ) , controlPoint1: CGPointScaleMake ( 31.27 , 28.64 ) , controlPoint2: CGPointScaleMake ( 34.23 , 23.04 ) )
114- bubblePath. addCurve ( to: CGPointScaleMake ( 42.59 , 13.21 ) , controlPoint1: CGPointScaleMake ( 39.45 , 16.43 ) , controlPoint2: CGPointScaleMake ( 40.97 , 14.76 ) )
115- bubblePath. addCurve ( to: CGPointScaleMake ( 75 , 0 ) , controlPoint1: CGPointScaleMake ( 51.11 , 5.01 ) , controlPoint2: CGPointScaleMake ( 62.5 , 0 ) )
116- bubblePath. addCurve ( to: CGPointScaleMake ( 120.6 , 34.75 ) , controlPoint1: CGPointScaleMake ( 96.35 , 0 ) , controlPoint2: CGPointScaleMake ( 114.43 , 14.59 ) )
112+ let size = bubbleSize
113+ let sR = ( size. width - size. height) / 4
114+ let lR = size. height/ 2
115+
116+ let l1 = CGPoint ( x: sR, y: lR - sR)
117+ let l2 = CGPoint ( x: sR, y: lR + sR)
118+
119+ let c1 = CGPoint ( x: sR * 2 + lR, y: 0 )
120+ let c2 = CGPoint ( x: sR * 2 + lR, y: lR * 2 )
121+
122+ let r1 = CGPoint ( x: sR * 3 + lR * 2 , y: lR - sR)
123+ let r2 = CGPoint ( x: sR * 3 + lR * 2 , y: lR + sR)
124+
125+ let o1 = CGPoint ( x: ( lR + sR * 2 ) / 4 , y: lR - sR)
126+ let o2 = CGPoint ( x: ( lR + sR * 2 ) / 4 , y: lR + sR)
127+ let o3 = CGPoint ( x: ( lR * 2 + sR * 4 ) - ( lR + sR * 2 ) / 4 , y: lR - sR)
128+ let o4 = CGPoint ( x: ( lR * 2 + sR * 4 ) - ( lR + sR * 2 ) / 4 , y: lR + sR)
129+
130+ // let cL = CGPoint(x: sR, y: lR)
131+ let cC = CGPoint ( x: sR * 2 + lR, y: lR)
132+ // let cR = CGPoint(x: sR * 3 + lR * 2, y: lR)
133+
134+ bubblePath. move ( to: l1)
135+ bubblePath. addQuadCurve ( to: c1, controlPoint: o1)
136+ bubblePath. addArc ( withCenter: cC, radius: lR, startAngle: - CGFloat. pi/ 2 , endAngle: CGFloat . pi*3/ 2 , clockwise: true )
137+ bubblePath. addQuadCurve ( to: r1, controlPoint: o3)
138+ bubblePath. addLine ( to: r2)
139+
140+ bubblePath. addQuadCurve ( to: c2, controlPoint: o4)
141+ bubblePath. addQuadCurve ( to: l2, controlPoint: o2)
142+ bubblePath. addLine ( to: l1)
117143 bubblePath. close ( )
144+
118145 return bubblePath. cgPath
119146 }
120147
@@ -132,7 +159,7 @@ extension TKLiquidSwitch{
132159 let w = self . bounds. width
133160
134161 if isOn{
135- return CGPoint ( x: 0 , y: h/ 2 )
162+ return CGPoint ( x: 0 + bubbleSize . width , y: h/ 2 )
136163 } else {
137164 return CGPoint ( x: w, y: h/ 2 )
138165 }
0 commit comments