杨锴
7 天以前 ac84f81ca2311300b431c1bfb9f71253b59073f2
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
//  EmptyDataSetView+Extension.swift
//  EmptyDataSet-Swift
//
//  Created by YZF on 3/7/17.
//  Copyright © 2017年 Xiaoye. All rights reserved.
//
 
import Foundation
import UIKit
 
extension EmptyDataSetView {
    
    //MARK: - Data Source
    
    /// Asks the data source for the title of the dataset.
    /// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
    @discardableResult
    public func titleLabelString(_ attributedString: NSAttributedString?) -> Self {
        titleLabel.attributedText = attributedString
        return self
    }
    
    /// Asks the data source for the description of the dataset.
    /// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
    @discardableResult
    public func detailLabelString(_ attributedString: NSAttributedString?) -> Self {
        detailLabel.attributedText = attributedString
        return self
    }
    
    /// Asks the data source for the image of the dataset.
    @discardableResult
    public func image(_ image: UIImage?) -> Self {
        imageView.image = image
        return self
    }
    
    /// Asks the data source for a tint color of the image dataset. Default is nil.
    @discardableResult
    public func imageTintColor(_ imageTintColor: UIColor?) -> Self {
        imageView.tintColor = imageTintColor
        return self
    }
    
    /// Asks the data source for the image animation of the dataset.
    @discardableResult
    public func imageAnimation(_ imageAnimation: CAAnimation?) -> Self {
        if let ani = imageAnimation {
            imageView.layer.add(ani, forKey: nil)
        }
        return self
    }
    
    /// Asks the data source for the title to be used for the specified button state.
    /// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
    @discardableResult
    public func buttonTitle(_ buttonTitle: NSAttributedString?, for state: UIControl.State) -> Self {
        button.setAttributedTitle(buttonTitle, for: state)
        return self
    }
    
    /// Asks the data source for the image to be used for the specified button state.
    /// This method will override buttonTitleForEmptyDataSet:forState: and present the image only without any text.
    @discardableResult
    public func buttonImage(_ buttonImage: UIImage?, for state: UIControl.State) -> Self {
        button.setImage(buttonImage, for: state)
        return self
    }
    
    /// Asks the data source for a background image to be used for the specified button state.
    /// There is no default style for this call.
    @discardableResult
    public func buttonBackgroundImage(_ buttonBackgroundImage: UIImage?, for state: UIControl.State) -> Self {
        button.setBackgroundImage(buttonBackgroundImage, for: state)
        return self
    }
    
    /// Asks the data source for the background color of the dataset. Default is clear color.
    @discardableResult
    public func dataSetBackgroundColor(_ backgroundColor: UIColor?) -> Self {
        self.backgroundColor = backgroundColor
        return self
    }
    
    /// Asks the data source for a custom view to be displayed instead of the default views such as labels, imageview and button. Default is nil.
    /// Use this method to show an activity view indicator for loading feedback, or for complete custom empty data set.
    /// Returning a custom view will ignore -offsetForEmptyDataSet and -spaceHeightForEmptyDataSet configurations.
    @discardableResult
    public func customView(_ customView: UIView?) -> Self {
        self.customView = customView
        return self
    }
    
    /// Asks the data source for a offset for vertical alignment of the content. Default is 0.
    @discardableResult
    public func verticalOffset(_ offset: CGFloat) -> Self {
        verticalOffset = offset
        return self
    }
    
    /// Asks the data source for a vertical space between elements. Default is 11 pts.
    @discardableResult
    public func verticalSpace(_ space: CGFloat) -> Self {
        verticalSpace = space
        return self
    }
    
    //MARK: - Delegate & Events
    /// Asks the delegate to know if the empty dataset should fade in when displayed. Default is true.
    @discardableResult
    public func shouldFadeIn(_ bool: Bool) -> Self {
        fadeInOnDisplay = bool
        return self
    }
    
    /// Asks the delegate to know if the empty dataset should still be displayed when the amount of items is more than 0. Default is false.
    @discardableResult
    public func shouldBeForcedToDisplay(_ bool: Bool) -> Self {
        isHidden = !bool
        return self
    }
    
    /// Asks the delegate to know if the empty dataset should be rendered and displayed. Default is true.
    @discardableResult
    public func shouldDisplay(_ bool: Bool) -> Self {
        if let superview = self.superview as? UIScrollView {
            isHidden = !(bool && superview.itemsCount == 0)
        }
        return self
    }
    
    /// Asks the delegate for touch permission. Default is true.
    @discardableResult
    public func isTouchAllowed(_ bool: Bool) -> Self {
        isUserInteractionEnabled = bool
        return self
    }
    
    /// Asks the delegate for scroll permission. Default is false.
    @discardableResult
    public func isScrollAllowed(_ bool: Bool) -> Self {
        if let superview = superview as? UIScrollView {
            superview.isScrollEnabled = bool
        }
        return self
    }
    
    /// Asks the delegate for image view animation permission. Default is false.
    /// Make sure to return a valid CAAnimation object from imageAnimationForEmptyDataSet:
    @discardableResult
    public func isImageViewAnimateAllowed(_ bool: Bool) -> Self {
        if !bool {
            imageView.layer.removeAllAnimations()
        }
        return self
    }
    
    /// Tells the delegate that the empty dataset view was tapped.
    /// Use this method either to resignFirstResponder of a textfield or searchBar.
    @discardableResult
    public func didTapContentView(_ closure: @escaping () -> (Void)) -> Self {
        didTapContentViewHandle = closure
        return self
    }
    
    /// Tells the delegate that the action button was tapped.
    @discardableResult
    public func didTapDataButton(_ closure: @escaping () -> (Void)) -> Self {
        didTapDataButtonHandle = closure
        return self
    }
    
    /// Tells the delegate that the empty data set will appear.
    @discardableResult
    public func willAppear(_ closure: @escaping () -> (Void)) -> Self {
        willAppearHandle = closure
        return self
    }
    
    /// Tells the delegate that the empty data set did appear.
    @discardableResult
    public func didAppear(_ closure: @escaping () -> (Void)) -> Self {
        didAppearHandle = closure
        return self
    }
    
    /// Tells the delegate that the empty data set will disappear.
    @discardableResult
    public func willDisappear(_ closure: @escaping () -> (Void)) -> Self {
        willDisappearHandle = closure
        return self
    }
    
    /// Tells the delegate that the empty data set did disappear.
    @discardableResult
    public func didDisappear(_ closure: @escaping () -> (Void)) -> Self {
        didDisappearHandle = closure
        return self
    }
    
}