Skip to content

Commit bc5d23b

Browse files
committed
[in-progress] add some classes and extesions
1 parent 7d4c7a3 commit bc5d23b

17 files changed

Lines changed: 1609 additions & 21 deletions

ESUIHelper.podspec

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Pod::Spec.new do |s|
2+
s.platforms = { :ios => "13.0", :osx => "10.15", :watchos => "6.0", :tvos => "13.0" }
3+
s.name = "ESUIHelper"
4+
s.summary = "ESUIHelper swift 5.2 UI framework for simple development amazing apps."
5+
s.requires_arc = true
6+
s.version = "1.0.0"
7+
s.license = { :type => "MIT", :file => "LICENSE" }
8+
s.author = { "Emil Karimov" => "emvakar@gmail.com" }
9+
s.homepage = "https://github.com/ESKARIA/ESUIHelper.git"
10+
s.source = { :git => "https://github.com/ESKARIA/ESUIHelper.git", :tag => "#{s.version}"}
11+
s.framework = "UIKit"
12+
s.source_files = "Sources/ESUIHelper/**/*.{swift}"
13+
s.resources = "Sources/ESUIHelper/**/*.{png,jpeg,jpg,storyboard,xib}"
14+
15+
#####
16+
# Waiting swift 5.3 for use bundle
17+
#####
18+
# s.resource_bundles = {
19+
# 'DevHelperAssets' => ['Sources/ESUIHelper/**/*.xcassets']
20+
# }
21+
s.swift_version = "5.2"
22+
end

Package.swift

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,17 @@ import PackageDescription
66
let package = Package(
77
name: "ESUIHelper",
88
platforms: [
9-
.iOS(.v11),
10-
.macOS(.v10_12),
11-
.watchOS(.v5),
12-
.tvOS(.v12)
9+
.iOS(.v13),
10+
.macOS(.v10_15),
11+
.watchOS(.v6),
12+
.tvOS(.v13)
1313
],
1414
products: [
15-
// Products define the executables and libraries produced by a package, and make them visible to other packages.
16-
.library(
17-
name: "ESUIHelper",
18-
targets: ["ESUIHelper"]),
19-
],
20-
dependencies: [
21-
// Dependencies declare other packages that this package depends on.
22-
// .package(url: /* package url */, from: "1.0.0"),
15+
.library(name: "ESUIHelper", targets: ["ESUIHelper"]),
2316
],
17+
dependencies: [],
2418
targets: [
25-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
26-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
27-
.target(
28-
name: "ESUIHelper",
29-
dependencies: []),
30-
.testTarget(
31-
name: "ESUIHelperTests",
32-
dependencies: ["ESUIHelper"]),
19+
.target(name: "ESUIHelper", dependencies: []),
20+
.testTarget(name: "ESUIHelperTests", dependencies: ["ESUIHelper"]),
3321
]
3422
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// ESAnimation.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 ESAnimation {
12+
13+
public static func animate(_ duration: TimeInterval, animations: (() -> Void)!, delay: TimeInterval = 0, options: UIView.AnimationOptions = [], withComplection completion: (() -> Void)! = { }) {
14+
15+
UIView.animate(
16+
withDuration: duration,
17+
delay: delay,
18+
options: options,
19+
animations: {
20+
animations()
21+
}, completion: { _ in
22+
completion()
23+
})
24+
}
25+
26+
public static func animateWithRepeatition(_ duration: TimeInterval, animations: (() -> Void)!, delay: TimeInterval = 0, options: UIView.AnimationOptions = [], withComplection completion: (() -> Void)! = { }) {
27+
28+
var optionsWithRepeatition = options
29+
optionsWithRepeatition.insert([.autoreverse, .repeat, .allowUserInteraction])
30+
31+
animate(
32+
duration,
33+
animations: {
34+
animations()
35+
},
36+
delay: delay,
37+
options: optionsWithRepeatition,
38+
withComplection: {
39+
completion()
40+
})
41+
}
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// ESAppleMusicButton.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+
open class ESAppleMusicButton: ESButton {
12+
13+
public var type: ESSelectionType = .unselect {
14+
didSet {
15+
updateType(animated: false)
16+
}
17+
}
18+
19+
public var selectColor = UIColor(hex: "FD2D55") {
20+
didSet {
21+
updateType(animated: false)
22+
}
23+
}
24+
25+
public var baseColor = UIColor(hex: "F8F7FC") {
26+
didSet {
27+
updateType(animated: false)
28+
}
29+
}
30+
31+
open override func commonInit() {
32+
super.commonInit()
33+
layer.cornerRadius = 8
34+
titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
35+
contentEdgeInsets = UIEdgeInsets.init(top: 12, left: 27, bottom: 12, right: 27)
36+
type = .unselect
37+
}
38+
39+
private func updateType(animated: Bool) {
40+
switch type {
41+
case .select:
42+
backgroundColor = selectColor
43+
setTitleColor(UIColor.white)
44+
case .unselect:
45+
backgroundColor = baseColor
46+
setTitleColor(selectColor)
47+
}
48+
}
49+
}

Sources/ESUIHelper/ESButton.swift

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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

Comments
 (0)