-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomButton.swift
executable file
·159 lines (129 loc) · 4.74 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
//
// CustomButton.swift
// Testing
//
// Created by Muhammad Khan on 1/20/18.
// Copyright © 2018 Muhammad Khan. All rights reserved.
//
import UIKit
@IBDesignable
class CustomButton: UIButton {
enum FromDirection:Int {
case Top = 0
case Right = 1
case Bottom = 2
case Left = 3
}
var shadowView: UIView!
var direction: FromDirection = .Left
var alphaBefore: CGFloat = 1
@IBInspectable var animate: Bool = false
@IBInspectable var animateDelay: Double = 0.2
@IBInspectable var animateFrom: Int {
get {
return direction.rawValue
}
set (directionIndex) {
direction = FromDirection(rawValue: directionIndex) ?? .Left
}
}
@IBInspectable var popIn: Bool = false
@IBInspectable var popInDelay: Double = 0.4
override func draw(_ rect: CGRect) {
self.clipsToBounds = true
if animate {
let originalFrame = frame
if direction == .Bottom {
frame = CGRect(x: frame.origin.x, y: frame.origin.y + 200, width: frame.width, height: frame.height)
}
UIView.animate(withDuration: 0.3, delay: animateDelay, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .allowUserInteraction, animations: {
self.frame = originalFrame
}, completion: nil)
}
if popIn {
transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
UIView.animate(withDuration: 0.8, delay: popInDelay, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .allowUserInteraction, animations: {
self.transform = CGAffineTransform.identity
}, completion: nil)
}
if shadowView == nil && shadowOpacity > 0 {
shadowView = UIView(frame: self.frame)
shadowView.backgroundColor = UIColor.clear
shadowView.layer.shadowColor = shadowColor.cgColor
shadowView.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: self.cornerRadius).cgPath
shadowView.layer.shadowOffset = shadowOffset
shadowView.layer.shadowOpacity = Float(shadowOpacity)
shadowView.layer.shadowRadius = shadowRadius
shadowView.layer.masksToBounds = true
shadowView.clipsToBounds = false
self.superview?.addSubview(shadowView)
self.superview?.bringSubview(toFront: self)
}
}
override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
alphaBefore = alpha
UIView.animate(withDuration: 0.2, delay: 0, options: .allowUserInteraction, animations: {
self.alpha = 0.4
})
return true
}
override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
UIView.animate(withDuration: 0.35, delay: 0, options: .allowUserInteraction, animations: {
self.alpha = self.alphaBefore
})
}
// MARK: - Borders
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}
// MARK: - Shadow
@IBInspectable public var shadowOpacity: CGFloat = 0
@IBInspectable public var shadowColor: UIColor = UIColor.clear
@IBInspectable public var shadowRadius: CGFloat = 0
@IBInspectable public var shadowOffset: CGSize = CGSize(width: 0, height: 0)
// MARK: - Gradient
@IBInspectable var firstColor: UIColor = UIColor.white {
didSet {
updateView()
}
}
@IBInspectable var secondColor: UIColor = UIColor.white {
didSet {
updateView()
}
}
@IBInspectable var horizontalGradient: Bool = false {
didSet {
updateView()
}
}
override public class var layerClass: AnyClass {
get {
return CAGradientLayer.self
}
}
func updateView() {
let layer = self.layer as! CAGradientLayer
layer.colors = [ firstColor.cgColor, secondColor.cgColor ]
if (horizontalGradient) {
layer.startPoint = CGPoint(x: 0.0, y: 0.5)
layer.endPoint = CGPoint(x: 1.0, y: 0.5)
} else {
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 0, y: 1)
}
}
}