/
UIButton+Ext.swift
107 lines (96 loc) · 4.33 KB
/
UIButton+Ext.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
//
// UIButton+Ext.swift
// ImageX
//
// Created by Condy on 2023/6/29.
//
import Foundation
import Harbeth
#if canImport(UIKit) && !os(watchOS)
import UIKit
extension UIButton: AsAnimatable, UIButtonContainer, ImageXCompatible { }
extension ImageXEngine where Base: UIButton {
/// Sets an image or gif to the button for a specified state with a named, And add the filters.
/// - Parameters:
/// - named: Picture or gif name.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
public func setImage(
with named: String?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) {
let other = Others(key: Others.UIButtonKey.image.rawValue, value: state)
Driver.setImage(named: named, to: base, options: options, other: other)
}
/// Sets an image or gif to the button for a specified state with a data, And add the filters.
/// - Parameters:
/// - data: Picture data.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
/// - Returns: A uniform type identifier UTI.
@discardableResult public func setImage(
with data: Data?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) -> AssetType {
let other = Others(key: Others.UIButtonKey.image.rawValue, value: state)
return Driver.setImage(data: data, to: base, options: options, other: other)
}
/// Sets an image or gif to the button for a specified state with a url, And add the filters.
/// - Parameters:
/// - url: Link url.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
/// - Returns: Current network URLSessionDataTask.
@discardableResult public func setImage(
with url: URL?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) -> ImageX.Task? {
let other = Others(key: Others.UIButtonKey.image.rawValue, value: state)
return Driver.setImage(url: url, to: base, options: options, other: other)
}
/// Sets an image or gif to the button for a specified state with a named, And add the filters.
/// - Parameters:
/// - named: Picture or gif name.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
public func setBackgroundImage(
with named: String?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) {
let other = Others(key: Others.UIButtonKey.backgroundImage.rawValue, value: state)
Driver.setImage(named: named, to: base, options: options, other: other)
}
/// Sets an image or gif to the button for a specified state with a data, And add the filters.
/// - Parameters:
/// - data: Picture data.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
/// - Returns: A uniform type identifier UTI.
@discardableResult public func setBackgroundImage(
with data: Data?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) -> AssetType {
let other = Others(key: Others.UIButtonKey.backgroundImage.rawValue, value: state)
return Driver.setImage(data: data, to: base, options: options, other: other)
}
/// Sets an image or gif to the button for a specified state with a url, And add the filters.
/// - Parameters:
/// - url: Link url.
/// - state: The button state to which the image or gif should be set.
/// - options: Represents creating options used in ImageX.
/// - Returns: Current network URLSessionDataTask.
@discardableResult public func setBackgroundImage(
with url: URL?,
for state: UIControl.State,
options: ImageXOptions = ImageXOptions.default
) -> ImageX.Task? {
let other = Others(key: Others.UIButtonKey.backgroundImage.rawValue, value: state)
return Driver.setImage(url: url, to: base, options: options, other: other)
}
}
#endif