forked from knn90/Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNSwitcher.swift
144 lines (117 loc) · 5.21 KB
/
KNSwitcher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Switcher.swift
// SwitcherExample
//
// Created by Khoi Nguyen Nguyen on 11/2/15.
// Copyright © 2015 Khoi Nguyen Nguyen. All rights reserved.
//
import UIKit
protocol KNSwitcherChangeValueDelegate {
func switcherDidChangeValue(KNSwitch:KNSwitcher,value:Bool)
}
class KNSwitcher: UIView {
var button: UIButton!
var delegate: KNSwitcherChangeValueDelegate?
var on:Bool = false
var selectedColor:UIColor = UIColor(red: 126/255.0, green: 134/255.0, blue: 249/255.0, alpha: 1)
var originalColor:UIColor = UIColor(red: 243/255.0, green: 229/255.0, blue: 211/255.0, alpha: 1)
var originalImage:UIImage = UIImage(named: "Delete")!
var selectedImage:UIImage = UIImage(named: "Checkmark")!
private var offCenterPosition:CGPoint!
private var onCenterPosition:CGPoint!
init(frame: CGRect,on: Bool) {
super.init(frame: frame)
self.on = on
self.backgroundColor = UIColor.white
button = UIButton(type: .custom) // let preferred over var here
button.frame = CGRect(x: 0, y:0 , width: frame.height * 0.75, height: frame.height * 0.75 )
button.center = CGPoint(x: button.frame.width/2 + 5, y: frame.height/2)
button.addTarget(self, action: #selector(switcherButtonTouch(_:)), for: UIControlEvents.touchUpInside)
button.setImage(originalImage, for: .normal)
button.setImage(selectedImage, for: .selected)
onCenterPosition = CGPoint(x:self.frame.width - (button.frame.width/2) - 5 , y: self.frame.height/2)
offCenterPosition = CGPoint(x: button.frame.width/2 + 5 , y: self.frame.height/2)
animationSwitcherButton()
self.addSubview(button)
}
func setImages(onImage:UIImage , offImage :UIImage)
{
button.setImage(offImage, for: .normal)
button.setImage(onImage, for: .selected)
}
func setOn(on:Bool) {
if(on != self.on)
{
self.on = on
animationSwitcherButton()
}
else
{
self.on = on
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
self.layer.cornerRadius = self.frame.height / 2
self.clipsToBounds = true
button.layer.cornerRadius = button.frame.height / 2
if on == true {
self.button.backgroundColor = selectedColor
} else {
self.button.backgroundColor = originalColor
}
}
func switcherButtonTouch(_ sender: AnyObject) {
on = !on;
animationSwitcherButton()
delegate?.switcherDidChangeValue(KNSwitch: self, value: on)
}
func animationSwitcherButton() {
if on == true {
// Rotate animation
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = -CGFloat(M_PI)
rotateAnimation.toValue = 0.0
rotateAnimation.duration = 0.45
rotateAnimation.isCumulative = false;
self.button.layer.add(rotateAnimation, forKey: "rotate")
// Translation animation
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
self.button.isSelected = true
self.button.center = self.onCenterPosition
self.layoutIfNeeded()
self.button.backgroundColor = self.selectedColor
}, completion: { (finish:Bool) -> Void in
self.button.layer.shadowOffset = CGSize(width: 0, height: 0.2)
self.button.layer.shadowOpacity = 0.3
self.button.layer.shadowRadius = 5.0
self.button.layer.cornerRadius = self.button.frame.height / 2
self.button.layer.shadowPath = UIBezierPath(roundedRect: self.button.layer.bounds, cornerRadius: self.button.frame.height / 2).cgPath
})
} else {
// Clear Shadow
self.button.layer.shadowOffset = CGSize.zero
self.button.layer.shadowOpacity = 0
self.button.layer.shadowRadius = self.button.frame.height / 2
self.button.layer.cornerRadius = self.button.frame.height / 2
self.button.layer.shadowPath = nil
// Rotate animation
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = -CGFloat(M_PI)
rotateAnimation.duration = 0.45
rotateAnimation.isCumulative = false;
self.button.layer.add(rotateAnimation, forKey: "rotate")
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
self.button.isSelected = false
self.button.center = self.offCenterPosition
self.layoutIfNeeded()
self.button.backgroundColor = self.originalColor
}, completion: { (finish:Bool) -> Void in
})
}
}
}