-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVVCompositeButton.swift
222 lines (187 loc) · 7.98 KB
/
VVCompositeButton.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
//
// CompositeButton.swift
// Composite Button
//
// Created by Kostantin Zarubin on 10/12/2018.
// Copyright © 2018 Kostantin Zarubin. All rights reserved.
//
import UIKit
protocol CompositeButtonDelegate: class {
func firstButtonSelected()
func secondButtonSelected()
func thirdButtonSelected()
}
@IBDesignable public class VVCompositeButton: UIButton {
var firstButton: VVCompositeButton?
var secondButton: VVCompositeButton?
var thirdButton: VVCompositeButton?
weak var delegate: CompositeButtonDelegate?
@IBInspectable var cornerRadius: CGFloat = 20 {
didSet {
refreshCorners(value: cornerRadius)
}
}
@IBInspectable var borderWidth: CGFloat = 2 {
didSet {
refreshBorder(borderWidth: borderWidth)
}
}
@IBInspectable var customBorderColor: UIColor = UIColor.init (red: 0, green: 122/255, blue: 255/255, alpha: 1){
didSet {
refreshBorderColor(colorBorder: customBorderColor)
}
}
@IBInspectable open var animatedScaleWhenSelected: CGFloat = 1.5
@IBInspectable open var animatedScaleDurationWhenSelected: Double = 0.2
@IBInspectable var leftSide: Bool = false
@IBInspectable open var selectedImage: UIImage?
@IBInspectable open var unselectedImage: UIImage?
@IBInspectable open var selectedState: Bool = true
func refreshBorder(borderWidth: CGFloat) {
layer.borderWidth = borderWidth
}
func refreshBorderColor(colorBorder: UIColor) {
layer.borderColor = colorBorder.cgColor
}
func refreshCorners(value: CGFloat) {
layer.cornerRadius = value
}
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
override public func prepareForInterfaceBuilder() {
sharedInit()
}
func sharedInit() {
self.clipsToBounds = true
self.setImage(unselectedImage, for: .normal)
refreshCorners(value: cornerRadius)
refreshBorderColor(colorBorder: customBorderColor)
refreshBorder(borderWidth: borderWidth)
}
override open var isHighlighted: Bool {
didSet {
if isHighlighted {
if isSelected {
UIView.animate(withDuration: 0.1) {
self.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 0.3)
}
} else {
UIView.animate(withDuration: 0.1) {
self.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 0.8)
}
}
}
else {
UIView.animate(withDuration: 0.1) {
self.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1)
}
}
}
}
override open var isSelected: Bool {
didSet {
if selectedState == true {
self.setImage(selectedImage, for: .selected)
self.addTarget(self, action: #selector(buttonSelected), for: .touchUpInside)
}
guard animatedScaleWhenSelected != 1.0 else { return }
UIView.animate(withDuration: animatedScaleDurationWhenSelected, animations: {
self.transform = CGAffineTransform(scaleX: self.animatedScaleWhenSelected, y: self.animatedScaleWhenSelected)
}) { (finished) in
UIView.animate(withDuration: self.animatedScaleDurationWhenSelected, animations: {
self.transform = CGAffineTransform.identity
if self.isSelected {
UIView.animate(withDuration: 0.1) {
self.backgroundColor = UIColor.white
}
self.refreshBorderColor(colorBorder: UIColor.gray)
} else {
UIView.animate(withDuration: 0.1) {
self.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1)
}
self.refreshBorderColor(colorBorder: self.customBorderColor)
}
})
}
}
}
@objc func buttonSelected(sender:AnyObject) {
self.isSelected = !self.isSelected
if isSelected {
addButtons()
UIView.animate(withDuration: 0.25) {
self.firstButton?.alpha = 1
self.secondButton?.alpha = 1
self.thirdButton?.alpha = 1
if self.leftSide {
self.firstButton?.center.x = self.center.x+100
self.secondButton?.center.x = self.center.x+70
self.secondButton?.center.y = self.center.y-70
self.thirdButton?.center.y = self.center.y-100
} else {
self.firstButton?.center.x = self.center.x-100
self.secondButton?.center.x = self.center.x-70
self.secondButton?.center.y = self.center.y-70
self.thirdButton?.center.y = self.center.y-100
}
}
} else {
removeButtons()
}
if #available(iOS 10.0, *) {
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
}
}
//MARK:- Configure additional Buttons
@IBInspectable open var firstButtonImage: UIImage?
@IBInspectable open var secondButtonImage: UIImage?
@IBInspectable open var thirdButtonImage: UIImage?
func addButtons() {
firstButton = VVCompositeButton(frame: CGRect(x: self.center.x, y: self.center.y-20, width: 40, height: 40))
firstButton?.alpha = 0
firstButton?.setImage(firstButtonImage, for: .normal)
firstButton!.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1)
firstButton!.addTarget(self, action: #selector(firstButtonAction), for: .touchUpInside)
self.superview?.addSubview(firstButton!)
secondButton = VVCompositeButton(frame: CGRect(x: self.center.x-20, y: self.center.y, width: 40, height: 40))
secondButton?.alpha = 0
secondButton?.setImage(secondButtonImage, for: .normal)
secondButton!.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1)
secondButton!.addTarget(self, action: #selector(secondButtonAction), for: .touchUpInside)
self.superview?.addSubview(secondButton!)
thirdButton = VVCompositeButton(frame: CGRect(x: self.center.x-20, y: self.center.y-20, width: 40, height: 40))
thirdButton?.alpha = 0
thirdButton?.setImage(thirdButtonImage, for: .normal)
thirdButton!.backgroundColor = UIColor(red: 0, green: 122/255, blue: 255/255, alpha: 1)
thirdButton!.addTarget(self, action: #selector(thirdButtonAction), for: .touchUpInside)
self.superview?.addSubview(thirdButton!)
}
@objc func firstButtonAction(sender: UIButton!) {
delegate?.firstButtonSelected()
}
@objc func secondButtonAction(sender: UIButton!) {
delegate?.secondButtonSelected()
}
@objc func thirdButtonAction(sender: UIButton!) {
delegate?.thirdButtonSelected()
}
func removeButtons() {
UIView.animate(withDuration: 0.25, animations: {
self.firstButton?.alpha = 0
self.firstButton?.center.x = self.center.x
self.secondButton?.alpha = 0
self.secondButton?.center.x = self.center.x
self.secondButton?.center.y = self.center.y-20
self.thirdButton?.alpha = 0
self.thirdButton?.center.y = self.center.y
}, completion: { _ in
self.firstButton?.removeFromSuperview()
})
}
}