Skip to content

Commit 1c80610

Browse files
committed
Update structure
1 parent 26c1c55 commit 1c80610

File tree

3 files changed

+154
-7
lines changed

3 files changed

+154
-7
lines changed

KNSwitcher.swift KNSwitcher/KNSwitcher.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010

1111
protocol KNSwitcherChangeValueDelegate {
12-
func switcherDidChangeValue(switcher:KNSwitcher,value:Bool)
12+
func switcherDidChangeValue(switcher: KNSwitcher,value: Bool)
1313
}
1414

1515
class KNSwitcher: UIView {
@@ -19,10 +19,8 @@ class KNSwitcher: UIView {
1919
var delegate: KNSwitcherChangeValueDelegate?
2020

2121
@IBInspectable var on: Bool = false
22-
2322
@IBInspectable var originalImage:UIImage?
2423
@IBInspectable var selectedImage:UIImage?
25-
2624
@IBInspectable var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1)
2725
@IBInspectable var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1)
2826

SwitcherExample.xcodeproj/project.pbxproj

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
7E1424761E7FB0630078B9D7 /* KNSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */; };
1011
D6BDB3991BE768030009E292 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB3981BE768030009E292 /* AppDelegate.swift */; };
1112
D6BDB39B1BE768030009E292 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB39A1BE768030009E292 /* ViewController.swift */; };
1213
D6BDB39E1BE768030009E292 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB39C1BE768030009E292 /* Main.storyboard */; };
1314
D6BDB3A01BE768030009E292 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB39F1BE768030009E292 /* Assets.xcassets */; };
1415
D6BDB3A31BE768030009E292 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D6BDB3A11BE768030009E292 /* LaunchScreen.storyboard */; };
15-
D6BDB3B11BE772360009E292 /* KNSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6BDB3B01BE772360009E292 /* KNSwitcher.swift */; };
1616
/* End PBXBuildFile section */
1717

1818
/* Begin PBXFileReference section */
19+
7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KNSwitcher.swift; sourceTree = "<group>"; };
1920
D6BDB3951BE768030009E292 /* SwitcherExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwitcherExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
2021
D6BDB3981BE768030009E292 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2122
D6BDB39A1BE768030009E292 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
2223
D6BDB39D1BE768030009E292 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
2324
D6BDB39F1BE768030009E292 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2425
D6BDB3A21BE768030009E292 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2526
D6BDB3A41BE768030009E292 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
26-
D6BDB3B01BE772360009E292 /* KNSwitcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = KNSwitcher.swift; path = ../KNSwitcher.swift; sourceTree = "<group>"; };
2727
/* End PBXFileReference section */
2828

2929
/* Begin PBXFrameworksBuildPhase section */
@@ -37,6 +37,14 @@
3737
/* End PBXFrameworksBuildPhase section */
3838

3939
/* Begin PBXGroup section */
40+
7E1424741E7FB0630078B9D7 /* KNSwitcher */ = {
41+
isa = PBXGroup;
42+
children = (
43+
7E1424751E7FB0630078B9D7 /* KNSwitcher.swift */,
44+
);
45+
path = KNSwitcher;
46+
sourceTree = "<group>";
47+
};
4048
D6BDB38C1BE768020009E292 = {
4149
isa = PBXGroup;
4250
children = (
@@ -62,7 +70,7 @@
6270
D6BDB39F1BE768030009E292 /* Assets.xcassets */,
6371
D6BDB3A11BE768030009E292 /* LaunchScreen.storyboard */,
6472
D6BDB3A41BE768030009E292 /* Info.plist */,
65-
D6BDB3B01BE772360009E292 /* KNSwitcher.swift */,
73+
7E1424741E7FB0630078B9D7 /* KNSwitcher */,
6674
);
6775
path = SwitcherExample;
6876
sourceTree = "<group>";
@@ -139,8 +147,8 @@
139147
buildActionMask = 2147483647;
140148
files = (
141149
D6BDB39B1BE768030009E292 /* ViewController.swift in Sources */,
150+
7E1424761E7FB0630078B9D7 /* KNSwitcher.swift in Sources */,
142151
D6BDB3991BE768030009E292 /* AppDelegate.swift in Sources */,
143-
D6BDB3B11BE772360009E292 /* KNSwitcher.swift in Sources */,
144152
);
145153
runOnlyForDeploymentPostprocessing = 0;
146154
};
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//
2+
// Switcher.swift
3+
// SwitcherExample
4+
//
5+
// Created by Khoi Nguyen Nguyen on 11/2/15.
6+
// Copyright © 2015 Khoi Nguyen Nguyen. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
protocol KNSwitcherChangeValueDelegate {
12+
func switcherDidChangeValue(switcher: KNSwitcher,value: Bool)
13+
}
14+
15+
class KNSwitcher: UIView {
16+
17+
var button: UIButton!
18+
var buttonLeftConstraint: NSLayoutConstraint!
19+
var delegate: KNSwitcherChangeValueDelegate?
20+
21+
@IBInspectable var on: Bool = false
22+
@IBInspectable var originalImage:UIImage?
23+
@IBInspectable var selectedImage:UIImage?
24+
@IBInspectable var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1)
25+
@IBInspectable var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1)
26+
27+
private var offCenterPosition: CGFloat!
28+
private var onCenterPosition: CGFloat!
29+
30+
init(frame: CGRect, on: Bool) {
31+
super.init(frame: frame)
32+
self.on = on
33+
commonInit()
34+
}
35+
36+
override func awakeFromNib() {
37+
commonInit()
38+
}
39+
40+
private func commonInit() {
41+
button = UIButton(type: .custom)
42+
self.addSubview(button)
43+
button.translatesAutoresizingMaskIntoConstraints = false
44+
button.addTarget(self, action: #selector(switcherButtonTouch(_:)), for: UIControlEvents.touchUpInside)
45+
button.setImage(originalImage, for: .normal)
46+
button.setImage(selectedImage, for: .selected)
47+
offCenterPosition = self.bounds.height * 0.1
48+
onCenterPosition = self.bounds.width - (self.bounds.height * 0.9)
49+
50+
if on == true {
51+
self.button.backgroundColor = selectedColor
52+
} else {
53+
self.button.backgroundColor = originalColor
54+
}
55+
56+
if self.backgroundColor == nil {
57+
self.backgroundColor = .white
58+
}
59+
initLayout()
60+
animationSwitcherButton()
61+
}
62+
63+
override func layoutSubviews() {
64+
super.layoutSubviews()
65+
self.layer.cornerRadius = self.bounds.height / 2
66+
self.clipsToBounds = true
67+
button.layer.cornerRadius = button.bounds.height / 2
68+
}
69+
70+
private func initLayout() {
71+
button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
72+
buttonLeftConstraint = button.leftAnchor.constraint(equalTo: self.leftAnchor)
73+
buttonLeftConstraint.isActive = true
74+
button.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.8).isActive = true
75+
button.widthAnchor.constraint(equalTo: button.heightAnchor, multiplier: 1).isActive = true
76+
}
77+
78+
func setImages(onImage:UIImage? , offImage :UIImage?) {
79+
button.setImage(offImage, for: .normal)
80+
button.setImage(onImage, for: .selected)
81+
}
82+
83+
required init?(coder aDecoder: NSCoder) {
84+
super.init(coder: aDecoder)
85+
}
86+
87+
func switcherButtonTouch(_ sender: AnyObject) {
88+
on = !on
89+
animationSwitcherButton()
90+
delegate?.switcherDidChangeValue(switcher: self, value: on)
91+
}
92+
93+
func animationSwitcherButton() {
94+
if on == true {
95+
// Rotate animation
96+
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
97+
rotateAnimation.fromValue = -CGFloat(M_PI)
98+
rotateAnimation.toValue = 0.0
99+
rotateAnimation.duration = 0.45
100+
rotateAnimation.isCumulative = false;
101+
self.button.layer.add(rotateAnimation, forKey: "rotate")
102+
103+
// Translation animation
104+
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in
105+
self.button.isSelected = true
106+
self.buttonLeftConstraint.constant = self.onCenterPosition
107+
self.layoutIfNeeded()
108+
self.button.backgroundColor = self.selectedColor
109+
}, completion: { (finish:Bool) -> Void in
110+
self.button.layer.shadowOffset = CGSize(width: 0, height: 0.2)
111+
self.button.layer.shadowOpacity = 0.3
112+
self.button.layer.shadowRadius = self.offCenterPosition
113+
self.button.layer.cornerRadius = self.button.frame.height / 2
114+
self.button.layer.shadowPath = UIBezierPath(roundedRect: self.button.layer.bounds, cornerRadius: self.button.frame.height / 2).cgPath
115+
})
116+
} else {
117+
// Clear Shadow
118+
self.button.layer.shadowOffset = CGSize.zero
119+
self.button.layer.shadowOpacity = 0
120+
self.button.layer.shadowRadius = self.button.frame.height / 2
121+
self.button.layer.cornerRadius = self.button.frame.height / 2
122+
self.button.layer.shadowPath = nil
123+
124+
// Rotate animation
125+
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
126+
rotateAnimation.fromValue = 0.0
127+
rotateAnimation.toValue = -CGFloat(M_PI)
128+
rotateAnimation.duration = 0.45
129+
rotateAnimation.isCumulative = false;
130+
self.button.layer.add(rotateAnimation, forKey: "rotate")
131+
132+
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: { () -> Void in
133+
self.button.isSelected = false
134+
self.buttonLeftConstraint.constant = self.offCenterPosition
135+
self.layoutIfNeeded()
136+
self.button.backgroundColor = self.originalColor
137+
}, completion: { (finish:Bool) -> Void in
138+
})
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)