Skip to content

Commit fbdbb18

Browse files
author
TBXark
committed
Bug fixed
1 parent 1ceb179 commit fbdbb18

File tree

11 files changed

+120
-148
lines changed

11 files changed

+120
-148
lines changed

SwitcherCollection.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
430430
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
431431
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
432-
PRODUCT_BUNDLE_IDENTIFIER = TBXark.TKSwitcherCollection;
432+
PRODUCT_BUNDLE_IDENTIFIER = com.TBXark.TKSwitcherCollection;
433433
PRODUCT_NAME = "$(TARGET_NAME)";
434434
SKIP_INSTALL = YES;
435435
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@@ -456,7 +456,7 @@
456456
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
457457
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
458458
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
459-
PRODUCT_BUNDLE_IDENTIFIER = TBXark.TKSwitcherCollection;
459+
PRODUCT_BUNDLE_IDENTIFIER = com.TBXark.TKSwitcherCollection;
460460
PRODUCT_NAME = "$(TARGET_NAME)";
461461
SKIP_INSTALL = YES;
462462
SWIFT_VERSION = 3.0;
-531 Bytes
Binary file not shown.

SwitcherCollection/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.3.0</string>
18+
<string>1.3.3</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

SwitcherCollection/ViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ViewController: UIViewController {
1919
}
2020

2121
func animateSwitch(_ timer:Timer){
22-
switchArray[count].changeValue()
22+
let switcher = switchArray[count]
23+
switcher.setOn(switcher.isOn, animate: true)
2324
count += 1
2425
if count == (switchArray.count){
2526
count = 0

TKSwitcherCollection.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "TKSwitcherCollection"
3-
s.version = "1.3.1"
3+
s.version = "1.3.3"
44
s.summary = "A animate switcher cllection in Swift."
55
s.license = { :type => 'MIT License', :file => 'LICENSE' } # 协议
66
s.homepage = "https://github.com/TBXark/TKSwitcherCollection"

TKSwitcherCollection/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.3.0</string>
18+
<string>1.3.3</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

TKSwitcherCollection/TKBaseSwitcher.swift

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88

99
import UIKit
1010

11-
public typealias ValueChangeHook = (_ value: Bool) -> Void
12-
13-
14-
protocol TKViewScale {}
15-
extension TKViewScale where Self: UIView {
16-
var sizeScale: CGFloat {
17-
return min(self.bounds.width, self.bounds.height)/100.0
18-
}
19-
}
11+
public typealias TKSwitchValueChangeHook = (_ value: Bool) -> Void
2012

2113
// 自定义 Switch 基类
2214
open class TKBaseSwitch: UIControl {
2315

2416
// MARK: - Property
25-
open var valueChange : ValueChangeHook?
17+
open var valueChange : TKSwitchValueChangeHook?
2618
open var animateDuration : Double = 0.4
27-
19+
open var isOn : Bool { return on }
20+
internal var on : Bool = true
21+
internal var sizeScale: CGFloat {
22+
return min(self.bounds.width, self.bounds.height)/100.0
23+
}
24+
25+
2826
open override var frame: CGRect {
2927
didSet {
3028
guard frame.size != oldValue.size else { return }
@@ -40,21 +38,32 @@ open class TKBaseSwitch: UIControl {
4038
}
4139

4240
// MARK: - Getter
43-
open var isOn : Bool{
44-
return on
45-
}
46-
internal var on : Bool = true
41+
4742

48-
func setOn(_ on: Bool, animate: Bool) {
49-
if on != isOn {
50-
changeValue()
51-
}
43+
final public func setOn(_ on: Bool, animate: Bool = true) {
44+
guard on != isOn else { return }
45+
changeValue()
46+
5247
}
5348

5449

50+
// MARK: - Init
5551
convenience public init() {
5652
self.init(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
5753
}
54+
55+
override public init(frame: CGRect) {
56+
super.init(frame: frame)
57+
setUpView()
58+
}
59+
60+
required public init?(coder aDecoder: NSCoder) {
61+
super.init(coder: aDecoder)
62+
setUpView()
63+
}
64+
65+
66+
// MARK: - Internal
5867

5968
internal func resetView() {
6069
gestureRecognizers?.forEach(self.removeGestureRecognizer)
@@ -67,12 +76,10 @@ open class TKBaseSwitch: UIControl {
6776
self.addGestureRecognizer(tap)
6877
}
6978

70-
public func changeValue(){
71-
if valueChange != nil{
72-
valueChange!(isOn)
73-
}
74-
sendActions(for: UIControlEvents.valueChanged);
79+
internal func changeValue(){
7580
on = !on
81+
valueChange?(!isOn)
82+
sendActions(for: UIControlEvents.valueChanged);
7683
}
7784

7885
}

TKSwitcherCollection/TKExchangeSwitch.swift

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

13-
open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
13+
open class TKExchangeSwitch: TKBaseSwitch {
1414

1515
// MARK: - Property
16-
fileprivate var swichControl : TKExchangeCircleView!
16+
fileprivate var swichControl : TKExchangeCircleView?
1717
fileprivate var backgroundLayer = CAShapeLayer()
1818

1919
open var color = (background: UIColor(white: 0.95, alpha: 1),
@@ -37,15 +37,6 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
3737
}
3838

3939
// MARK: - Init
40-
override public init(frame: CGRect) {
41-
super.init(frame: frame)
42-
setUpView()
43-
}
44-
45-
required public init?(coder aDecoder: NSCoder) {
46-
super.init(coder: aDecoder)
47-
setUpView()
48-
}
4940

5041
// MARK: - Private Func
5142
override internal func setUpView(){
@@ -56,21 +47,22 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
5647

5748
let backLayerPath = UIBezierPath()
5849
backLayerPath.move(to: CGPoint(x: lineWidth, y: 0))
59-
backLayerPath.addLine(to: CGPoint(x: self.bounds.width - 4 * lineWidth, y: 0))
50+
backLayerPath.addLine(to: CGPoint(x: bounds.width - 4 * lineWidth, y: 0))
6051

6152
backgroundLayer.position = position
6253
backgroundLayer.fillColor = color.background.cgColor
6354
backgroundLayer.strokeColor = color.background.cgColor
6455
backgroundLayer.lineWidth = self.bounds.height
6556
backgroundLayer.lineCap = kCALineCapRound
6657
backgroundLayer.path = backLayerPath.cgPath
67-
self.layer.addSublayer(backgroundLayer)
58+
layer.addSublayer(backgroundLayer)
6859

69-
let swichRadius = self.bounds.height - lineWidth
70-
swichControl = TKExchangeCircleView(frame: CGRect(x: lineWidth/2, y: lineWidth/2, width: swichRadius, height: swichRadius))
60+
let swichRadius = bounds.height - lineWidth
61+
let swichControl = TKExchangeCircleView(frame: CGRect(x: lineWidth/2, y: lineWidth/2, width: swichRadius, height: swichRadius))
7162
swichControl.onLayer.fillColor = color.on.cgColor
7263
swichControl.offLayer.fillColor = color.off.cgColor
73-
self.addSubview(swichControl)
64+
addSubview(swichControl)
65+
self.swichControl = swichControl
7466
}
7567

7668

@@ -83,7 +75,7 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
8375
open func changeValueAnimate(_ turnOn:Bool, duration:Double){
8476

8577
let keyTimes = [0,0.4,0.6,1]
86-
var frame = self.swichControl.frame
78+
guard var frame = self.swichControl?.frame else { return }
8779
frame.origin.x = turnOn ? lineWidth/2 : (self.bounds.width - self.bounds.height + lineWidth/2)
8880

8981
let swichControlStrokeStartAnim = CAKeyframeAnimation(keyPath:"strokeStart")
@@ -105,10 +97,10 @@ open class TKExchangeSwitch: TKBaseSwitch, TKViewScale {
10597
swichControlChangeStateAnim.duration = duration
10698

10799
backgroundLayer.add(swichControlChangeStateAnim, forKey: "SwitchBackground")
108-
swichControl.exchangeAnimate(turnOn, duration: duration)
100+
swichControl?.exchangeAnimate(turnOn, duration: duration)
109101

110102
UIView.animate(withDuration: duration, animations: { () -> Void in
111-
self.swichControl.frame = frame
103+
self.swichControl?.frame = frame
112104
})
113105
}
114106
}

TKSwitcherCollection/TKLiquidSwitch.swift

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)