Skip to content

Commit 808c0cb

Browse files
committed
Add IBDesignable support
1 parent b6bc5f3 commit 808c0cb

File tree

10 files changed

+93
-61
lines changed

10 files changed

+93
-61
lines changed

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- TKSwitcherCollection (1.4.0)
2+
- TKSwitcherCollection (1.4.1)
33

44
DEPENDENCIES:
55
- TKSwitcherCollection (from `../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: ../
1010

1111
SPEC CHECKSUMS:
12-
TKSwitcherCollection: 4992ce6e9f5752777e1efb6ef8f778aae21001fc
12+
TKSwitcherCollection: 22ffd357b16193be738e646e9256ba1a26942e17
1313

1414
PODFILE CHECKSUM: 22d4901429738fec3b90d800b97955912bfd129c
1515

Example/Pods/Local Podspecs/TKSwitcherCollection.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/TKSwitcherCollection/Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/TKSwitcherCollection/Base.lproj/Main.storyboard

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<constraints>
7070
<constraint firstAttribute="width" constant="90" id="3SK-WF-eCF"/>
7171
</constraints>
72+
<userDefinedRuntimeAttributes>
73+
<userDefinedRuntimeAttribute type="boolean" keyPath="rotateWhenValueChange" value="YES"/>
74+
</userDefinedRuntimeAttributes>
7275
</view>
7376
</subviews>
7477
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>

TKSwitcherCollection/TKBaseSwitcher.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,26 @@ import UIKit
1111
public typealias TKSwitchValueChangeHook = (_ value: Bool) -> Void
1212

1313
// 自定义 Switch 基类
14+
@IBDesignable
1415
open class TKBaseSwitch: UIControl {
1516

1617
// MARK: - Property
1718
open var valueChange : TKSwitchValueChangeHook?
18-
open var animateDuration : Double = 0.4
19-
open var isOn : Bool { return on }
19+
@IBInspectable open var animateDuration : Double = 0.4
20+
@IBInspectable open var isOn : Bool {
21+
set {
22+
on = newValue
23+
}
24+
get {
25+
return on
26+
}
27+
}
28+
2029
internal var on : Bool = true
2130
internal var sizeScale: CGFloat {
2231
return min(self.bounds.width, self.bounds.height)/100.0
2332
}
2433

25-
2634
open override var frame: CGRect {
2735
didSet {
2836
guard frame.size != oldValue.size else { return }

TKSwitcherCollection/TKExchangeSwitch.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,33 @@ import UIKit
1010
// Dedign by Oleg Frolov
1111
//https://dribbble.com/shots/2238916-Switcher-VI
1212

13+
@IBDesignable
1314
open class TKExchangeSwitch: TKBaseSwitch {
1415

1516
// MARK: - Property
16-
fileprivate var swichControl : TKExchangeCircleView?
17-
fileprivate var backgroundLayer = CAShapeLayer()
18-
19-
open var color = (background: UIColor(white: 0.95, alpha: 1),
20-
on: UIColor(red:0.698, green:1, blue:0.353, alpha:1),
21-
off: UIColor(red:0.812, green:0.847, blue:0.863, alpha:1)) {
22-
didSet {
23-
setUpView()
24-
}
17+
private var swichControl : TKExchangeCircleView?
18+
private var backgroundLayer = CAShapeLayer()
19+
20+
@IBInspectable open var lineColor = UIColor(white: 0.95, alpha: 1) {
21+
didSet {
22+
setUpView()
23+
}
24+
}
25+
26+
@IBInspectable open var onColor = UIColor(red:0.698, green:1, blue:0.353, alpha:1) {
27+
didSet {
28+
setUpView()
29+
}
30+
}
31+
32+
@IBInspectable open var offColor = UIColor(red:0.812, green:0.847, blue:0.863, alpha:1) {
33+
didSet {
34+
setUpView()
35+
}
2536
}
2637

2738

28-
open var lineSize : Double = 20 {
39+
@IBInspectable open var lineSize : Double = 20 {
2940
didSet {
3041
setUpView()
3142
}
@@ -50,17 +61,17 @@ open class TKExchangeSwitch: TKBaseSwitch {
5061
backLayerPath.addLine(to: CGPoint(x: bounds.width - 4 * lineWidth, y: 0))
5162

5263
backgroundLayer.position = position
53-
backgroundLayer.fillColor = color.background.cgColor
54-
backgroundLayer.strokeColor = color.background.cgColor
64+
backgroundLayer.fillColor = lineColor.cgColor
65+
backgroundLayer.strokeColor = lineColor.cgColor
5566
backgroundLayer.lineWidth = self.bounds.height
5667
backgroundLayer.lineCap = kCALineCapRound
5768
backgroundLayer.path = backLayerPath.cgPath
5869
layer.addSublayer(backgroundLayer)
5970

6071
let swichRadius = bounds.height - lineWidth
6172
let swichControl = TKExchangeCircleView(frame: CGRect(x: lineWidth/2, y: lineWidth/2, width: swichRadius, height: swichRadius))
62-
swichControl.onLayer.fillColor = color.on.cgColor
63-
swichControl.offLayer.fillColor = color.off.cgColor
73+
swichControl.onLayer.fillColor = onColor.cgColor
74+
swichControl.offLayer.fillColor = offColor.cgColor
6475
addSubview(swichControl)
6576
self.swichControl = swichControl
6677
}

TKSwitcherCollection/TKLiquidSwitch.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ import UIKit
1111
// Dedign by Oleg Frolov
1212
//https://dribbble.com/shots/2028065-Switcher-lll
1313

14+
@IBDesignable
1415
open class TKLiquidSwitch: TKBaseSwitch {
1516

1617

17-
fileprivate var bubbleLayer = CAShapeLayer()
18-
fileprivate var lineLayer = CAShapeLayer()
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)) {
18+
private var bubbleLayer = CAShapeLayer()
19+
private var lineLayer = CAShapeLayer()
20+
@IBInspectable open var onColor = UIColor(red:0.373, green:0.843, blue:0.596, alpha:1){
21+
didSet {
22+
setUpView()
23+
}
24+
}
25+
26+
@IBInspectable open var offColor = UIColor(red:0.871, green:0.871, blue:0.871, alpha:1) {
2127
didSet {
2228
setUpView()
2329
}
@@ -125,10 +131,10 @@ extension TKLiquidSwitch{
125131

126132
func switchColor(_ isOn: Bool)-> UIColor {
127133
if isOn{
128-
return color.on
134+
return onColor
129135
}
130136
else{
131-
return color.off
137+
return offColor
132138
}
133139
}
134140

TKSwitcherCollection/TKSimpleSwitch.swift

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,44 @@ import UIKit
1414

1515

1616

17-
public struct TKSimpleSwitchConfig {
18-
public var onColor : UIColor
19-
public var offColor : UIColor
20-
public var lineColor : UIColor
21-
public var circleColor : UIColor
22-
public var lineSize: Double
23-
24-
25-
26-
public init(onColor : UIColor = UIColor(red:0.341, green:0.914, blue:0.506, alpha:1),
27-
offColor : UIColor = UIColor(white: 0.9, alpha: 1),
28-
lineColor : UIColor = UIColor(white: 0.8, alpha: 1),
29-
circleColor : UIColor = UIColor.white,
30-
lineSize : Double = 10) {
31-
self.onColor = onColor
32-
self.offColor = offColor
33-
self.lineColor = lineColor
34-
self.circleColor = circleColor
35-
self.lineSize = lineSize
36-
37-
38-
}
39-
40-
}
4117

18+
@IBDesignable
4219
open class TKSimpleSwitch: TKBaseSwitch {
4320

4421
fileprivate var swichControl = CAShapeLayer()
4522
fileprivate var backgroundLayer = CAShapeLayer()
4623

4724
// 是否加旋转特效
48-
open var rotateWhenValueChange = false
25+
@IBInspectable open var rotateWhenValueChange: Bool = false
4926

50-
open var config = TKSimpleSwitchConfig() {
27+
@IBInspectable open var onColor : UIColor = UIColor(red:0.341, green:0.914, blue:0.506, alpha:1) {
28+
didSet {
29+
setUpView()
30+
}
31+
}
32+
@IBInspectable open var offColor : UIColor = UIColor(white: 0.9, alpha: 1) {
33+
didSet {
34+
setUpView()
35+
}
36+
}
37+
@IBInspectable open var lineColor : UIColor = UIColor(white: 0.8, alpha: 1) {
38+
didSet {
39+
setUpView()
40+
}
41+
}
42+
@IBInspectable open var circleColor : UIColor = UIColor.white {
43+
didSet {
44+
setUpView()
45+
}
46+
}
47+
@IBInspectable open var lineSize: Double = 10 {
5148
didSet {
5249
setUpView()
5350
}
5451
}
5552

56-
open var lineWidth : CGFloat {
57-
return CGFloat(config.lineSize) * sizeScale
53+
private var lineWidth : CGFloat {
54+
return CGFloat(lineSize) * sizeScale
5855
}
5956

6057

@@ -66,7 +63,7 @@ open class TKSimpleSwitch: TKBaseSwitch {
6663
let radius = bounds.height/2 - lineWidth
6764
let roundedRectPath = UIBezierPath(roundedRect:frame.insetBy(dx: lineWidth, dy: lineWidth) , cornerRadius:radius)
6865
backgroundLayer.fillColor = stateToFillColor(true)
69-
backgroundLayer.strokeColor = config.lineColor.cgColor
66+
backgroundLayer.strokeColor = lineColor.cgColor
7067
backgroundLayer.lineWidth = lineWidth
7168
backgroundLayer.path = roundedRectPath.cgPath
7269
layer.addSublayer(backgroundLayer)
@@ -82,7 +79,7 @@ open class TKSimpleSwitch: TKBaseSwitch {
8279
swichControl.path = swichControlPath.cgPath
8380
swichControl.lineCap = kCALineCapRound
8481
swichControl.fillColor = nil
85-
swichControl.strokeColor = config.circleColor.cgColor
82+
swichControl.strokeColor = circleColor.cgColor
8683
swichControl.lineWidth = innerLineWidth
8784
swichControl.strokeEnd = 0.0001
8885
layer.addSublayer(swichControl)
@@ -152,7 +149,7 @@ open class TKSimpleSwitch: TKBaseSwitch {
152149
}
153150

154151
fileprivate func stateToFillColor(_ isOn:Bool) -> CGColor {
155-
return isOn ? config.onColor.cgColor : config.offColor.cgColor
152+
return isOn ? onColor.cgColor : offColor.cgColor
156153
}
157154

158155
}

0 commit comments

Comments
 (0)