81 lines
1.8 KiB
Swift
81 lines
1.8 KiB
Swift
import UIKit
|
|
|
|
enum ACButtonStyle {
|
|
case generic
|
|
case roundedBlue
|
|
}
|
|
|
|
class ACButton: UIButton {
|
|
|
|
private var style: ACButtonStyle = .generic
|
|
|
|
convenience init(style: ACButtonStyle = .roundedBlue, title: String, onTap: @escaping () -> Void) {
|
|
self.init()
|
|
self.style(style)
|
|
self.onTap(onTap)
|
|
self.title(title)
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
convenience init(style: ACButtonStyle = .roundedBlue, title: String, onTapAsync: @escaping () async -> Void) {
|
|
self.init()
|
|
self.style(style)
|
|
self.onTapAsync(onTapAsync)
|
|
self.title(title)
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
if style == .roundedBlue {
|
|
self.layer.opacity = self.isEnabled ? 1 : 0.5
|
|
} else {
|
|
self.layer.opacity = 1
|
|
}
|
|
}
|
|
|
|
@discardableResult
|
|
func style(_ style: ACButtonStyle) -> ACButton {
|
|
|
|
self.style = style
|
|
|
|
switch style {
|
|
case .generic:
|
|
break
|
|
case .roundedBlue:
|
|
backgroundColor = .systemBlue
|
|
layer.cornerRadius = 6
|
|
}
|
|
|
|
return self
|
|
}
|
|
|
|
@discardableResult
|
|
func title(_ title: String) -> ACButton {
|
|
|
|
setTitle(title, for: .normal)
|
|
return self
|
|
}
|
|
|
|
@discardableResult
|
|
func enable(_ enabled: Bool) -> ACButton {
|
|
isEnabled = enabled
|
|
return self
|
|
}
|
|
|
|
@discardableResult
|
|
func onTap(_ handler: @escaping () -> Void) -> ACButton {
|
|
|
|
addAction(for: .touchUpInside, handler)
|
|
return self
|
|
}
|
|
|
|
@discardableResult
|
|
func onTapAsync(_ handler: @escaping () async -> Void) -> ACButton {
|
|
|
|
addActionAsync(for: .touchUpInside, handler)
|
|
return self
|
|
}
|
|
}
|