-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomButton.swift
175 lines (152 loc) · 4.97 KB
/
CustomButton.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
//
// DoroButton.swift
// Connect And Care
//
// Created by Adrian Ziemecki on 29/03/16.
// Copyright © 2016 Doro AB. All rights reserved.
//
import UIKit
let kFontSizeDefault: CGFloat = 20
let kVerticalPaddingDefault: CGFloat = 8
let kItemsHorizontalSpacingDefault: CGFloat = 0
let kCornerRadiusDefault: CGFloat = -1
let kBorderWidthDefault: CGFloat = 0
let kBorderColorDefault = UIColor.clearColor()
@IBDesignable
class DoroButton: UIButton {
//var height: NSLayoutConstraint!
let hiliteLayer: CALayer
let disabledLayer: CALayer
@IBInspectable var fontSize: CGFloat = kFontSizeDefault {
didSet {
titleLabel?.font = UIFont.boldSystemFontOfSize(fontSize)
}
}
@IBInspectable var verticalPadding: CGFloat? {
didSet {
if let padding = verticalPadding {
contentEdgeInsets.top += padding
contentEdgeInsets.bottom += padding
}
}
}
@IBInspectable var itemsHorizontalSpacing: CGFloat = kItemsHorizontalSpacingDefault {
didSet {
if (self.imageView?.image == nil) {
itemsHorizontalSpacing = 0
}
contentEdgeInsets.right += itemsHorizontalSpacing
titleEdgeInsets.left += itemsHorizontalSpacing
titleEdgeInsets.right += -itemsHorizontalSpacing
}
}
// @IBInspectable var cornerRadius: CGFloat = kCornerRadiusDefault {
// didSet {
// if cornerRadius >= 0 {
// layer.cornerRadius = cornerRadius
// } else {
// layer.cornerRadius = frame.height/2
// }
// layer.masksToBounds = true
// }
// }
@IBInspectable var borderWidth: CGFloat = 1 {
didSet {
layer.borderWidth = borderWidth/UIScreen.mainScreen().scale
}
}
@IBInspectable var borderColor: UIColor = UIColor.clearColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
override init(frame: CGRect) {
hiliteLayer = CALayer()
disabledLayer = CALayer()
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
hiliteLayer = CALayer()
disabledLayer = CALayer()
super.init(coder: aDecoder)
setupViews()
}
func setupViews() {
titleLabel?.textAlignment = .Center
if backgroundColor == nil {
backgroundColor = UIColor.grayColor()
}
fontSize = kFontSizeDefault
if verticalPadding == nil {
verticalPadding = kVerticalPaddingDefault
}
configureHighliteLayer()
configureDisabledLayer()
}
override func prepareForInterfaceBuilder() {
setupViews()
}
override func drawRect(rect: CGRect) {
roundCorners()
}
func roundCorners() {
layer.cornerRadius = frame.height / 2
layer.masksToBounds = true
titleLabel?.font = UIFont.boldSystemFontOfSize(fontSize)
}
private func configureHighliteLayer() {
hiliteLayer.frame = layer.bounds
hiliteLayer.opacity = 1
hiliteLayer.backgroundColor = UIColor(white: 0.0, alpha: 0.2).CGColor
}
private func configureDisabledLayer() {
disabledLayer.frame = layer.bounds
disabledLayer.opacity = 1
disabledLayer.backgroundColor = UIColor(white: 0.5, alpha: 0.5).CGColor
}
override var highlighted: Bool {
set {
if highlighted != newValue {
super.highlighted = newValue
if newValue {
layer.addSublayer(hiliteLayer)
let scale = CABasicAnimation(keyPath: "transform.scale")
scale.fromValue = 1
scale.toValue = 0.98
scale.duration = 0.1
scale.removedOnCompletion = false
scale.fillMode = kCAFillModeForwards
layer.addAnimation(scale, forKey: "scale")
} else {
let scale = CABasicAnimation(keyPath: "transform.scale")
scale.fromValue = 0.98
scale.toValue = 1
scale.duration = 0.1
scale.removedOnCompletion = false
scale.fillMode = kCAFillModeForwards
layer.addAnimation(scale, forKey: "scale")
hiliteLayer.removeFromSuperlayer()
}
}
}
get {
return super.highlighted
}
}
override var enabled: Bool {
set {
if enabled != newValue {
super.enabled = newValue
if newValue {
disabledLayer.removeFromSuperlayer()
} else {
layer.addSublayer(disabledLayer)
}
}
}
get {
return super.enabled
}
}
}