杨锴
2025-05-11 7453d2d0cef415b34323d1b91e6cfa4a6ba31178
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
//  JJPageControl.swift
//  JJPageControl
//  
//  https://github.com/zxhkit/JJPageControl
//  Created by xuanhe on 2022/9/1.
//
 
import UIKit
 
enum JJPageControlAliment {
    case Center
    case Left
    case Right
}
 
protocol JJPageControlDelegate {
    func jj_pageControlClick(pageControl: JJPageControl, index: Int)
}
 
class JJPageControl: UIControl {
    
    /// 闭包点击事件
    var clickPointClosure: ((_ num : Int) -> Void)?
 
    /// 当前点的大小
    var currentPointSize: CGSize = CGSize(width: 6, height: 6) {
        didSet{
            assert((currentPointSize.width >= 1 && currentPointSize.height >= 1), "Parameter value is not valid. currentPointSize must be greater than 1.")
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 其他点的大小
    var otherPointSize: CGSize = CGSize(width: 6, height: 6) {
        didSet{
            assert((otherPointSize.width >= 1 && otherPointSize.height >= 1), "Parameter value is not valid. otherPointSize must be greater than 1.")
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 点-切圆角
    var pointCornerRadius:CGFloat = 3 {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 位置
    var pageAliment: JJPageControlAliment = .Center {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 分页数量
    var numberOfPages: Int = 0 {
        didSet{
            if oldValue != numberOfPages {
                createPointView()
            }
        }
    }
    
    /// 当前点所在下标
    var currentPage: Int = 0 {
        didSet{
            if currentPage >= 0 {
                exchangePointView(oldValue, currentPage)
            }else{
                currentPage = 0
            }
        }
    }
    ///点的间距
    var controlSpacing: CGFloat = 8 {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    ///左右间距
    var leftAndRightSpacing: CGFloat = 10 {
        didSet{
            if leftAndRightSpacing < 0 {
                leftAndRightSpacing = 0
            }else{
                if numberOfPages > 0 {
                    createPointView()
                }
            }
        }
    }
    /// 其他点未选中颜色
    var otherColor: UIColor = .gray {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 当前点颜色
    var currentColor: UIColor = .orange {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 其他点背景图片
    var otherBkImage: UIImage? {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 当前点背景图片
    var currentBkImage: UIImage? {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    ///当前选中点的layer宽
    var currentLayerBorderWidth: CGFloat = 1 {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    
    ///其他点的layer宽
    var otherLayerBorderWidth: CGFloat = 1 {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    
    ///当前选中点的layer颜色
    var currentLayerBorderColor: UIColor? {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    ///其他选中点的layer颜色
    var otherLayerBorderColor: UIColor? {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    
    
    /// 当只有一个点的时候是否隐藏
    var isHidesForSinglePage = false {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    /// 是否可以点击 默认不可以点击
    var isCanClickPoint = false {
        didSet{
            if numberOfPages > 0 {
                createPointView()
            }
        }
    }
    
    /// 重写设置frame
    override var frame:CGRect {
        didSet{
            let mainWidth = (CGFloat(numberOfPages) - 1) * otherPointSize.width + (CGFloat(numberOfPages) - 1) * controlSpacing + currentPointSize.width + leftAndRightSpacing * 2
 
            if frame.width < mainWidth {
                frame.size.width = mainWidth
            }
            let max_height = max(otherPointSize.height, currentPointSize.height)
            if frame.height < max_height {
                frame.size.height = max_height
            }
            createPointView()
        }
    }
    
    /// 协议代理
    var delegate:JJPageControlDelegate?
        
    private var dots = [UIImageView]()
    
    /// 创建视图
    private func createPointView() {
        /// 先清除视图
        clearView()
        
        if numberOfPages <= 0 {
            return
        }
        
        if isHidesForSinglePage == true ,numberOfPages == 1{
            return
        }
        //居中控件
        var startX:CGFloat = 0
        var startY_current:CGFloat = 0
        var startY_other:CGFloat = 0
 
        
        let mainWidth = (CGFloat(numberOfPages) - 1) * otherPointSize.width + (CGFloat(numberOfPages) - 1) * controlSpacing + currentPointSize.width + leftAndRightSpacing * 2 + 1
        if frame.width < mainWidth {
            frame.size.width = mainWidth
        }else{
            if pageAliment == .Left {
                startX = leftAndRightSpacing
            } else if pageAliment == .Center {
                startX = (self.frame.size.width - mainWidth)/2.0
            } else {
                startX = frame.width - mainWidth
            }
        }
        
        let max_height = max(otherPointSize.height, currentPointSize.height)
        if frame.height < max_height {
            frame.size.height = max_height
            startY_current = max_height - currentPointSize.height
            startY_other = max_height - otherPointSize.height
 
        } else {
            startY_current = (frame.height - max_height)/2.0 + (max_height - currentPointSize.height)
            startY_other = (frame.height - max_height)/2.0 + (max_height - otherPointSize.height)
        }
        
        for page in 0..<numberOfPages {
            if page == currentPage {
                let currentPoint = UIImageView(frame: CGRect(x: startX, y: startY_current, width: currentPointSize.width, height: currentPointSize.height))
                currentPoint.layer.masksToBounds = true
                currentPoint.layer.cornerRadius = pointCornerRadius
                currentPoint.tag = 1000 + page
                currentPoint.backgroundColor = currentColor
                currentPoint.isUserInteractionEnabled = true
                currentPoint.image = currentBkImage
                
                if self.currentLayerBorderColor != nil {
                    currentPoint.layer.borderColor = self.currentLayerBorderColor?.cgColor
                    currentPoint.layer.borderWidth = self.currentLayerBorderWidth
                }else{
                    currentPoint.layer.borderWidth = 0
                }
                
                if isCanClickPoint == true {
                    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickAction(_:)))
                    currentPoint.addGestureRecognizer(tapGesture)
                }
                
                addSubview(currentPoint)
                
                startX = currentPoint.frame.maxX + controlSpacing
                if let _ = currentBkImage {
                    currentPoint.backgroundColor = .clear
                }
                dots.append(currentPoint)
            }else{
                let otherPoint = UIImageView(frame: CGRect(x: startX, y: startY_other, width: otherPointSize.width, height: otherPointSize.height))
                otherPoint.layer.masksToBounds = true
                otherPoint.layer.cornerRadius = pointCornerRadius
                otherPoint.tag = 1000 + page
                otherPoint.backgroundColor = otherColor
                otherPoint.isUserInteractionEnabled = true
                otherPoint.image = otherBkImage
                
                if self.otherLayerBorderColor != nil {
                    otherPoint.layer.borderColor = self.otherLayerBorderColor?.cgColor
                    otherPoint.layer.borderWidth = self.otherLayerBorderWidth
                }else{
                    otherPoint.layer.borderWidth = 0
                }
                
                if isCanClickPoint == true {
                    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickAction(_:)))
                    otherPoint.addGestureRecognizer(tapGesture)
                }
                addSubview(otherPoint)
                
                startX = otherPoint.frame.maxX + controlSpacing
                
                if let _ = otherBkImage {
                    otherPoint.backgroundColor = .clear
                }
                dots.append(otherPoint)
            }
        }
    }
    
    
    @objc private func clickAction(_ recognizer: UITapGestureRecognizer) {
        
        if let tag = recognizer.view?.tag {
            let index = tag - 1000
            if index >= 0 {
                currentPage = index
                delegate?.jj_pageControlClick(pageControl: self, index: index)
                if let closure = self.clickPointClosure {
                    closure(index)
                }
            }
        }
    }
    
    
    
    func clearView() {
         subviews.forEach { subView in
             subView.removeFromSuperview()
         }
        
        subviews.forEach { $0.removeFromSuperview()}
        dots.removeAll()
    }
    
    func exchangePointView(_ oldPage: Int, _ newPage: Int) {
        if oldPage == newPage {
            return
        }
        if oldPage > dots.count {
            return
        }
        if newPage > dots.count {
            return
        }
        
        
        let oldDot = dots[oldPage]
        let oldFrame = oldDot.frame
        
        let newDot = dots[newPage]
        let newFrame = newDot.frame
        
        newDot.image = currentBkImage
        if let _ = currentBkImage {
            newDot.backgroundColor = .clear
        }else{
            newDot.backgroundColor = currentColor
        }
        
        oldDot.image = otherBkImage
        if let _ = otherBkImage {
            oldDot.backgroundColor = .clear
        }else{
            oldDot.backgroundColor = otherColor
        }
        
        if self.currentLayerBorderColor != nil {
            newDot.layer.borderColor = self.currentLayerBorderColor?.cgColor
            newDot.layer.borderWidth = self.currentLayerBorderWidth
        }else{
            newDot.layer.borderWidth = 0
        }
        
        if self.otherLayerBorderColor != nil {
            oldDot.layer.borderColor = self.otherLayerBorderColor?.cgColor
            oldDot.layer.borderWidth = self.otherLayerBorderWidth
        }else{
            oldDot.layer.borderWidth = 0
        }
        
        var oldMinx = oldFrame.minX
        var newMinx = newFrame.minX
        
        UIView.animate(withDuration: 0.25) {
            if newPage < oldPage {
                oldMinx = oldMinx + (self.currentPointSize.width - self.otherPointSize.width)
            }
            oldDot.frame = CGRect(x: oldMinx, y: newFrame.minY, width: self.otherPointSize.width, height: self.otherPointSize.height)
            
            if newPage > oldPage {
                newMinx = newMinx - (self.currentPointSize.width - self.otherPointSize.width)
            }
            newDot.frame = CGRect(x: newMinx, y: oldFrame.minY, width: self.currentPointSize.width, height: self.currentPointSize.height)
            
            
            if newPage - oldPage > 1 {  //往右滑动
                for index in (oldPage+1)..<newPage {
                    if index < self.dots.count {
                        let point = self.dots[index]
                        point.frame = CGRect(x: point.frame.minX - (self.currentPointSize.width - self.otherPointSize.width),
                                             y: point.frame.minY,
                                             width: self.otherPointSize.width,
                                             height: self.otherPointSize.height)
                    }else{
                        return
                    }
                }
            }
            
            if newPage - oldPage < -1 {  //往左滑动
                for index in (newPage+1)..<oldPage {
                    if index < self.dots.count {
                        let point = self.dots[index]
                        point.frame = CGRect(x: point.frame.minX + (self.currentPointSize.width - self.otherPointSize.width),
                                             y: point.frame.minY,
                                             width: self.otherPointSize.width,
                                             height: self.otherPointSize.height)
                    }else{
                        return
                    }
                }
            }
        } completion: { finshed in
            if finshed == false {  //执行代码和执行过程中代码一样
                if newPage < oldPage {
                    oldMinx = oldMinx + (self.currentPointSize.width - self.otherPointSize.width)
                }
                oldDot.frame = CGRect(x: oldMinx, y: newFrame.minY, width: self.otherPointSize.width, height: self.otherPointSize.height)
                
                if newPage > oldPage {
                    newMinx = newMinx - (self.currentPointSize.width - self.otherPointSize.width)
                }
                newDot.frame = CGRect(x: newMinx, y: oldFrame.minY, width: self.currentPointSize.width, height: self.currentPointSize.height)
                
                
                if newPage - oldPage > 1 {  //往右滑动
                    for index in (oldPage+1)..<newPage {
                        if index < self.dots.count {
                            let point = self.dots[index]
                            point.frame = CGRect(x: point.frame.minX - (self.currentPointSize.width - self.otherPointSize.width),
                                                 y: point.frame.minY,
                                                 width: self.otherPointSize.width,
                                                 height: self.otherPointSize.height)
                        }else{
                            return
                        }
                    }
                }
                
                if newPage - oldPage < -1 {  //往左滑动
                    for index in (newPage+1)..<oldPage {
                        if index < self.dots.count {
                            let point = self.dots[index]
                            point.frame = CGRect(x: point.frame.minX + (self.currentPointSize.width - self.otherPointSize.width),
                                                 y: point.frame.minY,
                                                 width: self.otherPointSize.width,
                                                 height: self.otherPointSize.height)
                        }else{
                            return
                        }
                    }
                }
            }
        }
    }
}