Skip to content

Commit 1ceb179

Browse files
author
TBXark
committed
fix TKLiquidSwitch bug
1 parent 0518332 commit 1ceb179

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

SwitcherCollection/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<constraint firstItem="GJr-HY-1Iz" firstAttribute="centerY" secondItem="ARs-yq-gFQ" secondAttribute="centerY" id="u6F-Ma-efH"/>
138138
</constraints>
139139
</view>
140-
<view hidden="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8Nl-xn-CAM" userLabel="cell">
140+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8Nl-xn-CAM" userLabel="cell">
141141
<rect key="frame" x="16" y="460" width="343" height="95"/>
142142
<subviews>
143143
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="TKLiquidSwitch" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="s3c-Xc-6Fl" userLabel="type">

TKSwitcherCollection/TKBaseSwitcher.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
import UIKit
1010

1111
public typealias ValueChangeHook = (_ value: Bool) -> Void
12-
internal func CGPointScaleMaker(_ scale: CGFloat) -> ((CGFloat, CGFloat) -> CGPoint) {
13-
return { (x, y) in
14-
return CGPoint(x: x * scale ,y: y * scale)}
15-
}
12+
1613

1714
protocol TKViewScale {}
1815
extension TKViewScale where Self: UIView {

TKSwitcherCollection/TKLiquidSwitch.swift

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
96101
extension 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

Comments
 (0)