|
| 1 | +// |
| 2 | +// ESButton.swift |
| 3 | +// ESUIHelper |
| 4 | +// |
| 5 | +// Created by Emil Karimov on 11/06/2020 |
| 6 | +// Copyright © 2020 ESKARIA LLC. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +public enum ESSelectionType { |
| 12 | + case select |
| 13 | + case unselect |
| 14 | +} |
| 15 | + |
| 16 | +open class ESButton: UIButton { |
| 17 | + |
| 18 | + public override func imageRect(forContentRect contentRect: CGRect) -> CGRect { |
| 19 | + if title(for: .normal) != nil { |
| 20 | + let inset: CGFloat = 6 |
| 21 | + let sideSize = frame.height - inset * 2 |
| 22 | + let titleFrame = titleRect(forContentRect: contentRect) |
| 23 | + return CGRect.init(x: titleFrame.origin.x - sideSize - 6, y: 0, width: sideSize, height: frame.height) |
| 24 | + } else { |
| 25 | + return super.imageRect(forContentRect: contentRect) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public override var isHighlighted: Bool { |
| 30 | + didSet { |
| 31 | + if isHighlighted { |
| 32 | + imageView?.alpha = 0.7 |
| 33 | + } else { |
| 34 | + imageView?.alpha = 1 |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public var rounded: Bool = false { |
| 40 | + didSet { |
| 41 | + layoutSubviews() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public init() { |
| 46 | + super.init(frame: CGRect.zero) |
| 47 | + commonInit() |
| 48 | + } |
| 49 | + |
| 50 | + required public init?(coder aDecoder: NSCoder) { |
| 51 | + super.init(coder: aDecoder) |
| 52 | + commonInit() |
| 53 | + } |
| 54 | + |
| 55 | + internal func commonInit() { |
| 56 | + adjustsImageWhenHighlighted = false |
| 57 | + } |
| 58 | + |
| 59 | + public override func layoutSubviews() { |
| 60 | + super.layoutSubviews() |
| 61 | + if rounded { |
| 62 | + round() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public func set(enable: Bool, animatable: Bool) { |
| 67 | + isEnabled = enable |
| 68 | + if animatable { |
| 69 | + ESAnimation.animate(0.3, animations: { |
| 70 | + self.alpha = enable ? 1 : 0.6 |
| 71 | + }) |
| 72 | + } else { |
| 73 | + alpha = enable ? 1 : 0.6 |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension UIButton { |
| 79 | + |
| 80 | + public typealias UIButtonTargetClosure = () -> Void |
| 81 | + |
| 82 | + private class ClosureWrapper: NSObject { |
| 83 | + let closure: UIButtonTargetClosure |
| 84 | + init(_ closure: @escaping UIButtonTargetClosure) { |
| 85 | + self.closure = closure |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private struct AssociatedKeys { |
| 90 | + static var targetClosure = "targetClosure" |
| 91 | + } |
| 92 | + |
| 93 | + private var targetClosure: UIButtonTargetClosure? { |
| 94 | + get { |
| 95 | + guard let closureWrapper = objc_getAssociatedObject(self, &AssociatedKeys.targetClosure) as? ClosureWrapper else { return nil } |
| 96 | + return closureWrapper.closure |
| 97 | + } |
| 98 | + set(newValue) { |
| 99 | + guard let newValue = newValue else { return } |
| 100 | + objc_setAssociatedObject(self, &AssociatedKeys.targetClosure, ClosureWrapper(newValue), objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public func target(_ action: @escaping UIButtonTargetClosure) { |
| 105 | + targetClosure = action |
| 106 | + addTarget(self, action: #selector(UIButton.targetAction), for: .touchUpInside) |
| 107 | + } |
| 108 | + |
| 109 | + @objc func targetAction() { |
| 110 | + guard let targetClosure = targetClosure else { return } |
| 111 | + targetClosure() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +extension UIButton { |
| 116 | + |
| 117 | + public func setTitle(_ title: String, color: UIColor? = nil) { |
| 118 | + setTitle(title, for: .normal) |
| 119 | + if let color = color { |
| 120 | + setTitleColor(color) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public func setTitleColor(_ color: UIColor) { |
| 125 | + setTitleColor(color, for: .normal) |
| 126 | + setTitleColor(color.withAlphaComponent(0.7), for: .highlighted) |
| 127 | + } |
| 128 | + |
| 129 | + public func setImage(_ image: UIImage) { |
| 130 | + setImage(image, for: .normal) |
| 131 | + imageView?.contentMode = .scaleAspectFit |
| 132 | + } |
| 133 | + |
| 134 | + public func removeAllTargets() { |
| 135 | + removeTarget(nil, action: nil, for: .allEvents) |
| 136 | + } |
| 137 | + |
| 138 | + public func showText(_ text: String, withComplection completion: (() -> Void)! = {}) { |
| 139 | + let baseText = titleLabel?.text ?? " " |
| 140 | + ESAnimation.animate(0.2, animations: { |
| 141 | + self.titleLabel?.alpha = 0 |
| 142 | + }, withComplection: { |
| 143 | + self.setTitle(text, for: .normal) |
| 144 | + ESAnimation.animate(0.2, animations: { |
| 145 | + self.titleLabel?.alpha = 1 |
| 146 | + }, withComplection: { |
| 147 | + ESAnimation.animate(0.2, animations: { |
| 148 | + self.titleLabel?.alpha = 0 |
| 149 | + }, delay: 0.35, |
| 150 | + withComplection: { |
| 151 | + self.setTitle(baseText, for: .normal) |
| 152 | + ESAnimation.animate(0.2, animations: { |
| 153 | + self.titleLabel?.alpha = 1 |
| 154 | + }, withComplection: { |
| 155 | + completion() |
| 156 | + }) |
| 157 | + }) |
| 158 | + }) |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + public func setAnimatableText(_ text: String, withComplection completion: (() -> Void)! = {}) { |
| 163 | + ESAnimation.animate(0.3, animations: { |
| 164 | + self.titleLabel?.alpha = 0 |
| 165 | + }, withComplection: { |
| 166 | + self.setTitle(text, for: .normal) |
| 167 | + ESAnimation.animate(0.3, animations: { |
| 168 | + self.titleLabel?.alpha = 1 |
| 169 | + }, withComplection: { |
| 170 | + completion() |
| 171 | + }) |
| 172 | + }) |
| 173 | + } |
| 174 | + |
| 175 | + public func hideContent(completion: (() -> Void)! = {}) { |
| 176 | + ESAnimation.animate(0.25, animations: { |
| 177 | + self.titleLabel?.alpha = 0 |
| 178 | + }, withComplection: { |
| 179 | + completion() |
| 180 | + }) |
| 181 | + } |
| 182 | + |
| 183 | + public func showContent(completion: (() -> Void)! = {}) { |
| 184 | + ESAnimation.animate(0.25, animations: { |
| 185 | + self.titleLabel?.alpha = 1 |
| 186 | + }, withComplection: { |
| 187 | + completion() |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments