/
UIButton+Extensions.swift
235 lines (198 loc) · 6.76 KB
/
UIButton+Extensions.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// UIButton+Extensions.swift
//
//
// Created by Jobins John on 9/2/19.
//
import Foundation
extension UIButton {
//MARK: — wiggle() => Wiggles the button in x axis.
func wiggle() {
let wiggleAnim = CABasicAnimation(keyPath: "position")
wiggleAnim.duration = 0.05
wiggleAnim.repeatCount = 5
wiggleAnim.autoreverses = true
wiggleAnim.fromValue = CGPoint(x: self.center.x - 4.0, y: self.center.y)
wiggleAnim.toValue = CGPoint(x: self.center.x + 4.0, y: self.center.y)
layer.add(wiggleAnim, forKey: "position")
}
/*
Button.wiggle()
*/
//MARK: — dim() => Button color would gets dimmed and reverses.
func dim() {
UIView.animate(withDuration: 0.15, animations: {
self.alpha = 0.75
}) { (finished) in
UIView.animate(withDuration: 0.15, animations: {
self.alpha = 1.0
})
}
}
/*
Button.dim()
*/
//MARK: — colorize() => Provides a random color to button.
func colorize() {
let randomNumberArray = generateRandomnumbers(quantity: 3)
let randomColor = UIColor(red: randomNumberArray[0]/255, green: randomNumberArray[1]/255, blue: randomNumberArray[2]/255, alpha: 1.0)
UIView.animate(withDuration: 0.3) {
self.backgroundColor = randomColor
}
}
/*
Button.colorize()
*/
//MARK: — verticalAlignImageAndTitle() => Provides Button with image & Title vertically aligned.
func verticalAlignImageAndTitle(padding: CGFloat = 5.0, isTitleUnderImage: Bool = true) {
let imageSize = self.imageView!.frame.size
let titleSize = self.titleLabel!.frame.size
if isTitleUnderImage {
self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, -(imageSize.height + padding), 0)
self.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + padding), 0, 0, -titleSize.width)
} else {
self.titleEdgeInsets = UIEdgeInsetsMake(-(imageSize.height + padding), -imageSize.width, 0, 0)
self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, -(titleSize.height + padding), -titleSize.width)
}
}
/*
let myButton = UIButton()
myButton.setTitle("Button", for: .normal)
myButton.setImage(UIImage(named: "image_name"), for: .normal)
addSubview(myButton)
myButton.verticalAlignImageAndTitle(padding: 6.0, isTitleUnderImage: false)
*/
//MARK: — startLoading() => Start loading animation in button.
func startLoading(color: UIColor) {
let shapeOuter = shapeLayerOuter(color: color)
let shapeInner = shapeLayerInner(color: color)
shapeOuter.add(animationOuter(), forKey: nil)
shapeInner.add(animationInner(), forKey: nil)
self.layer.addSublayer(shapeOuter)
self.layer.addSublayer(shapeInner)
}
//MARK: — stopLoading() => Stop loading animation in button.
func stopLoading(){
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.alpha = 0
}) { (true) in
self.layer.removeFromSuperlayer()
}
}
//MARK: — stopLoading() => Stop loading animation in button.
func roundedButtonWithAvatarImage(image: UIImage, borderWidth: CGFloat = 1.0, borderColor: CGColor = UIColor.whiteColor().CGColor) {
self.setImage(image, forState: .Normal)
self.layer.cornerRadius = self.frame.size.width / 2
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor
self.clipsToBounds = true
}
func roundedButtonWithAvatarImageData(imageData: NSData, borderWidth: CGFloat = 1.0, borderColor: CGColor = UIColor.whiteColor().CGColor) {
self.roundedButtonWithAvatarImage(UIImage(data: imageData)!, borderWidth: borderWidth, borderColor: borderColor)
}
/*
Button.roundedButtonWithAvatarImage(UIImage(named: "image_name")!, borderWidth: 1, borderColor: UIColor.blueColor().CGColor)
*/
//MARK: — round() => Set corner radius for button.
func round() {
layer.cornerRadius = bounds.height / 2
clipsToBounds = true
}
/*
Button.round()
*/
//MARK: — bounce() => Make button bounce.
func bounce() {
UIView.animate(withDuration: 0.1, animations: {
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}) { (completion) in
UIView.animate(withDuration: 0.1, animations: {
self.transform = .identity
})
}
}
/*
Button.bounce()
*/
//MARK: — shine() => make button shine.
func shine() {
UIView.animate(withDuration: 0.1, animations: {
self.alpha = 0.5
}) { (completion) in
UIView.animate(withDuration: 0.1, animations: {
self.alpha = 1
})
}
}
/*
Button.shine()
*/
//MARK: — jump() => Makes button jump.
func jump() {
UIView.animate(withDuration: 0.1, animations: {
self.transform = CGAffineTransform(translationX: 0, y: -10)
}) { (completion) in
UIView.animate(withDuration: 0.1, animations: {
self.transform = .identity
})
}
}
/*
Button.jump()
*/
//MARK: — HELPER FUNCTIONS SECTION
//Helper functions for startLoading() & stopLoading()
private func shapeLayerOuter(color: UIColor) -> CAShapeLayer{
let circleOut = UIBezierPath(arcCenter: CGPoint(x: self.bounds.midX, y: self.bounds.midY),
radius: CGFloat(self.bounds.height * 0.4),
startAngle: CGFloat(0),
endAngle:CGFloat(Double.pi * 2),
clockwise: true)
let shapeLayerOut = CAShapeLayer()
shapeLayerOut.path = circleOut.cgPath
shapeLayerOut.fillColor = UIColor.clear.cgColor
shapeLayerOut.strokeColor = color.cgColor
shapeLayerOut.strokeStart = 0.1
shapeLayerOut.strokeEnd = 1
shapeLayerOut.lineWidth = 3.5
shapeLayerOut.lineCap = "round"
shapeLayerOut.frame = self.bounds
return shapeLayerOut
}
//Helper functions for startLoading() & stopLoading()
private func shapeLayerInner(color: UIColor) -> CAShapeLayer{
let circleIn = UIBezierPath(arcCenter: CGPoint(x: self.bounds.midX, y: self.bounds.midY),
radius: CGFloat(self.bounds.height * 0.2),
startAngle: CGFloat(0),
endAngle: CGFloat(Double.pi * 2),
clockwise: false)
let shapeLayerIn = CAShapeLayer()
shapeLayerIn.path = circleIn.cgPath
shapeLayerIn.fillColor = UIColor.clear.cgColor
shapeLayerIn.strokeColor = color.cgColor
shapeLayerIn.strokeStart = 0.1
shapeLayerIn.strokeEnd = 1
shapeLayerIn.lineWidth = 3.2
shapeLayerIn.lineCap = "round"
shapeLayerIn.frame = self.bounds
return shapeLayerIn
}
//Helper functions for startLoading() & stopLoading()
private func animationOuter() -> CABasicAnimation{
let animationOut = CABasicAnimation(keyPath: "transform.rotation")
animationOut.fromValue = 0
animationOut.toValue = Double.pi * 2
animationOut.duration = 1
animationOut.repeatCount = Float.infinity
return animationOut
}
//Helper functions for startLoading() & stopLoading()
private func animationInner() -> CABasicAnimation {
let animationIn = CABasicAnimation(keyPath: "transform.rotation")
animationIn.fromValue = 0
animationIn.toValue = -(Double.pi * 2)
animationIn.duration = 1
animationIn.repeatCount = Float.infinity
return animationIn
}
}