forked from WenchaoD/FSPagerView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSPageControl.swift
310 lines (274 loc) · 10.5 KB
/
FSPageControl.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// FSPageControl.swift
// FSPagerView
//
// Created by Wenchao Ding on 17/12/2016.
// Copyright © 2016 Wenchao Ding. All rights reserved.
//
import UIKit
@IBDesignable
open class FSPageControl: UIControl {
/// The number of page indicators of the page control. Default is 0.
@IBInspectable
open var numberOfPages: Int = 0 {
didSet {
self.setNeedsCreateIndicators()
}
}
/// The current page, highlighted by the page control. Default is 0.
@IBInspectable
open var currentPage: Int = 0 {
didSet {
self.setNeedsUpdateIndicators()
}
}
/// The spacing to use of page indicators in the page control.
@IBInspectable
open var itemSpacing: CGFloat = 6 {
didSet {
self.setNeedsUpdateIndicators()
}
}
/// The spacing to use between page indicators in the page control.
@IBInspectable
open var interitemSpacing: CGFloat = 6 {
didSet {
self.setNeedsLayout()
}
}
/// The distance that the page indicators is inset from the enclosing page control.
@IBInspectable
open var contentInsets: UIEdgeInsets = .zero {
didSet {
self.setNeedsLayout()
}
}
/// The horizontal alignment of content within the control’s bounds. Default is center.
open override var contentHorizontalAlignment: UIControl.ContentHorizontalAlignment {
didSet {
self.setNeedsLayout()
}
}
/// Hide the indicator if there is only one page. default is NO
@IBInspectable
open var hidesForSinglePage: Bool = false {
didSet {
self.setNeedsUpdateIndicators()
}
}
internal var strokeColors: [UIControl.State: UIColor] = [:]
internal var fillColors: [UIControl.State: UIColor] = [:]
internal var paths: [UIControl.State: UIBezierPath] = [:]
internal var images: [UIControl.State: UIImage] = [:]
internal var alphas: [UIControl.State: CGFloat] = [:]
internal var transforms: [UIControl.State: CGAffineTransform] = [:]
fileprivate weak var contentView: UIView!
fileprivate var needsUpdateIndicators = false
fileprivate var needsCreateIndicators = false
fileprivate var indicatorLayers = [CAShapeLayer]()
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
open override func layoutSubviews() {
super.layoutSubviews()
self.contentView.frame = {
let x = self.contentInsets.left
let y = self.contentInsets.top
let width = self.frame.width - self.contentInsets.left - self.contentInsets.right
let height = self.frame.height - self.contentInsets.top - self.contentInsets.bottom
let frame = CGRect(x: x, y: y, width: width, height: height)
return frame
}()
}
open override func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
let diameter = self.itemSpacing
let spacing = self.interitemSpacing
var x: CGFloat = {
switch self.contentHorizontalAlignment {
case .left, .leading:
return 0
case .center, .fill:
let midX = self.contentView.bounds.midX
let amplitude = CGFloat(self.numberOfPages/2) * diameter + spacing*CGFloat((self.numberOfPages-1)/2)
return midX - amplitude
case .right, .trailing:
let contentWidth = diameter*CGFloat(self.numberOfPages) + CGFloat(self.numberOfPages-1)*spacing
return contentView.frame.width - contentWidth
default:
return 0
}
}()
for (index,value) in self.indicatorLayers.enumerated() {
let state: UIControl.State = (index == self.currentPage) ? .selected : .normal
let image = self.images[state]
let size = image?.size ?? CGSize(width: diameter, height: diameter)
let origin = CGPoint(x: x - (size.width-diameter)*0.5, y: self.contentView.bounds.midY-size.height*0.5)
value.frame = CGRect(origin: origin, size: size)
x = x + spacing + diameter
}
}
/// Sets the stroke color for page indicators to use for the specified state. (selected/normal).
///
/// - Parameters:
/// - strokeColor: The stroke color to use for the specified state.
/// - state: The state that uses the specified stroke color.
@objc(setStrokeColor:forState:)
open func setStrokeColor(_ strokeColor: UIColor?, for state: UIControl.State) {
guard self.strokeColors[state] != strokeColor else {
return
}
self.strokeColors[state] = strokeColor
self.setNeedsUpdateIndicators()
}
/// Sets the fill color for page indicators to use for the specified state. (selected/normal).
///
/// - Parameters:
/// - fillColor: The fill color to use for the specified state.
/// - state: The state that uses the specified fill color.
@objc(setFillColor:forState:)
open func setFillColor(_ fillColor: UIColor?, for state: UIControl.State) {
guard self.fillColors[state] != fillColor else {
return
}
self.fillColors[state] = fillColor
self.setNeedsUpdateIndicators()
}
/// Sets the image for page indicators to use for the specified state. (selected/normal).
///
/// - Parameters:
/// - image: The image to use for the specified state.
/// - state: The state that uses the specified image.
@objc(setImage:forState:)
open func setImage(_ image: UIImage?, for state: UIControl.State) {
guard self.images[state] != image else {
return
}
self.images[state] = image
self.setNeedsUpdateIndicators()
}
@objc(setAlpha:forState:)
/// Sets the alpha value for page indicators to use for the specified state. (selected/normal).
///
/// - Parameters:
/// - alpha: The alpha value to use for the specified state.
/// - state: The state that uses the specified alpha.
open func setAlpha(_ alpha: CGFloat, for state: UIControl.State) {
guard self.alphas[state] != alpha else {
return
}
self.alphas[state] = alpha
self.setNeedsUpdateIndicators()
}
/// Sets the path for page indicators to use for the specified state. (selected/normal).
///
/// - Parameters:
/// - path: The path to use for the specified state.
/// - state: The state that uses the specified path.
@objc(setPath:forState:)
open func setPath(_ path: UIBezierPath?, for state: UIControl.State) {
guard self.paths[state] != path else {
return
}
self.paths[state] = path
self.setNeedsUpdateIndicators()
}
// MARK: - Private functions
fileprivate func commonInit() {
// Content View
let view = UIView(frame: .zero)
view.backgroundColor = UIColor.clear
self.addSubview(view)
self.contentView = view
self.isUserInteractionEnabled = false
}
fileprivate func setNeedsUpdateIndicators() {
self.needsUpdateIndicators = true
self.setNeedsLayout()
DispatchQueue.main.async {
self.updateIndicatorsIfNecessary()
}
}
fileprivate func updateIndicatorsIfNecessary() {
guard self.needsUpdateIndicators else {
return
}
guard self.indicatorLayers.count > 0 else {
return
}
self.needsUpdateIndicators = false
self.contentView.isHidden = self.hidesForSinglePage && self.numberOfPages <= 1
if !self.contentView.isHidden {
self.indicatorLayers.forEach { (layer) in
layer.isHidden = false
self.updateIndicatorAttributes(for: layer)
}
}
}
fileprivate func updateIndicatorAttributes(for layer: CAShapeLayer) {
let index = self.indicatorLayers.firstIndex(of: layer)
let state: UIControl.State = index == self.currentPage ? .selected : .normal
if let image = self.images[state] {
layer.strokeColor = nil
layer.fillColor = nil
layer.path = nil
layer.contents = image.cgImage
} else {
layer.contents = nil
let strokeColor = self.strokeColors[state]
let fillColor = self.fillColors[state]
if strokeColor == nil && fillColor == nil {
layer.fillColor = (state == .selected ? UIColor.white : UIColor.gray).cgColor
layer.strokeColor = nil
} else {
layer.strokeColor = strokeColor?.cgColor
layer.fillColor = fillColor?.cgColor
}
layer.path = self.paths[state]?.cgPath ?? UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: self.itemSpacing, height: self.itemSpacing)).cgPath
}
if let transform = self.transforms[state] {
layer.transform = CATransform3DMakeAffineTransform(transform)
}
layer.opacity = Float(self.alphas[state] ?? 1.0)
}
fileprivate func setNeedsCreateIndicators() {
self.needsCreateIndicators = true
DispatchQueue.main.async {
self.createIndicatorsIfNecessary()
}
}
fileprivate func createIndicatorsIfNecessary() {
guard self.needsCreateIndicators else {
return
}
self.needsCreateIndicators = false
CATransaction.begin()
CATransaction.setDisableActions(true)
if self.currentPage >= self.numberOfPages {
self.currentPage = self.numberOfPages - 1
}
self.indicatorLayers.forEach { (layer) in
layer.removeFromSuperlayer()
}
self.indicatorLayers.removeAll()
for _ in 0..<self.numberOfPages {
let layer = CAShapeLayer()
layer.actions = ["bounds": NSNull()]
self.contentView.layer.addSublayer(layer)
self.indicatorLayers.append(layer)
}
self.setNeedsUpdateIndicators()
self.updateIndicatorsIfNecessary()
CATransaction.commit()
}
}
extension UIControl.State: Hashable {
public var hashValue: Int {
return Int((6777*self.rawValue+3777)%UInt(UInt16.max))
}
}