This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
SYButton.swift
186 lines (157 loc) · 4.98 KB
/
SYButton.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
//
// SYButton.swift
// SYBlinkAnimationKit
//
// Created by Shohei Yokoyama on 12/13/2015.
// Copyright © 2015年 Shohei. All rights reserved.
//
import UIKit
@IBDesignable
public class SYButton: UIButton, AnimatableComponent, TextConvertible {
public enum AnimationType: Int {
case border, borderWithShadow, background, ripple, text
}
@IBInspectable public var animationBorderColor = AnimationDefaultColor.border {
didSet {
syLayer.setBorderColor(animationBorderColor)
}
}
@IBInspectable public var animationBackgroundColor = AnimationDefaultColor.background {
didSet {
syLayer.setAnimationBackgroundColor(animationBackgroundColor)
}
}
@IBInspectable public var animationTextColor = AnimationDefaultColor.text {
didSet {
syLayer.setAnimationTextColor(animationTextColor)
}
}
@IBInspectable public var animationRippleColor = AnimationDefaultColor.ripple {
didSet {
syLayer.setRippleColor(animationRippleColor)
}
}
@IBInspectable var animationTimingAdapter: Int {
get {
return animationTimingFunction.rawValue
}
set(index) {
animationTimingFunction = SYMediaTimingFunction(rawValue: index) ?? .linear
}
}
@IBInspectable public var animationDuration: CGFloat = 1.5 {
didSet {
syLayer.setAnimationDuration( CFTimeInterval(animationDuration) )
}
}
@IBInspectable var animationAdapter: Int {
get {
return animationType.rawValue
}
set(index) {
animationType = AnimationType(rawValue: index) ?? .border
}
}
public var animationTimingFunction: SYMediaTimingFunction = .linear {
didSet {
syLayer.setTimingFunction(animationTimingFunction)
}
}
public var animationType: AnimationType = .border {
didSet {
syLayer.animationType = {
switch animationType {
case .border:
return .border
case .borderWithShadow:
return .borderWithShadow
case .background:
return .background
case .ripple:
return .ripple
case .text:
return .text
}
}()
}
}
override public var bounds: CGRect {
didSet {
syLayer.resizeSuperLayer()
}
}
override public var frame: CGRect {
didSet {
syLayer.resizeSuperLayer()
}
}
override public var backgroundColor: UIColor? {
didSet {
if let backgroundColor = backgroundColor {
syLayer.setBackgroundColor(backgroundColor)
}
}
}
public var isAnimating = false
var textLayer = CATextLayer()
public var textAlignmentMode: TextAlignmentMode = .center {
didSet {
resetTextLayer()
}
}
fileprivate lazy var syLayer: SYLayer = .init(layer: self.layer)
fileprivate var textColor: UIColor = .black {
didSet {
syLayer.setTextColor(textColor)
}
}
// MARK: - initializer -
public override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
// MARK: - Override Methods -
override public func setTitle(_ title: String?, for state: UIControlState) {
super.setTitle(title ?? "", for: state)
resetTextLayer()
}
override public func setTitleColor(_ color: UIColor?, for state: UIControlState) {
super.setTitleColor(UIColor.clear, for: state)
if let color = color {
textLayer.foregroundColor = color.cgColor
textColor = color
}
}
// MARK: - Public Methods -
public func setFont(name fontName: String = ".SFUIText-Medium", ofSize fontSize: CGFloat) -> Self {
titleLabel?.font = UIFont(name: fontName, size: fontSize)
resetTextLayer()
return self
}
public func startAnimating() {
isAnimating = true
syLayer.startAnimating()
}
public func stopAnimating() {
isAnimating = false
syLayer.startAnimating()
}
}
// MARK: - Fileprivate Methods -
fileprivate extension SYButton {
func configure() {
layer.cornerRadius = 5
let padding: CGFloat = 5
contentEdgeInsets = UIEdgeInsets(top: padding, left: padding, bottom: padding, right: padding)
syLayer.animationType = .border
setTitleColor(.black, for: .normal)
}
func resetTextLayer() {
configureTextLayer(currentTitle, font: titleLabel?.font, textColor: textColor)
syLayer.resetTextLayer(textLayer)
}
}