forked from amirdew/CollectionViewPagingLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageControlView.swift
More file actions
108 lines (86 loc) · 2.6 KB
/
PageControlView.swift
File metadata and controls
108 lines (86 loc) · 2.6 KB
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
//
// PageControlView.swift
// CollectionViewPagingLayout
//
// Created by Amir Khorsandi on 12/24/19.
// Copyright © 2019 Amir Khorsandi. All rights reserved.
//
import UIKit
class PageControlView: UIView {
// MARK: Properties
var preferences = Preferences() {
didSet {
setNeedsDisplay()
}
}
var numberOfPages: Int = 0 {
didSet {
setNeedsDisplay()
}
}
var currentPage: Int = 0 {
didSet {
setNeedsDisplay()
}
}
// MARK: Life cycle
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
guard numberOfPages > 0 else {
return
}
drawDots(rect: rect)
}
override var intrinsicContentSize: CGSize {
CGSize(
width: CGFloat(numberOfPages) * (preferences.dotRadius * 2) + (CGFloat(numberOfPages) - 1) * preferences.gapSize + preferences.currentDotBorderWidth * 2,
height: preferences.dotRadius * 2 + preferences.currentDotBorderWidth * 2
)
}
// MARK: Private functions
private func commonInit() {
backgroundColor = .clear
}
private func drawDots(rect: CGRect) {
for index in 0..<numberOfPages {
let path = UIBezierPath(
arcCenter: .init(
x: preferences.dotRadius + preferences.currentDotBorderWidth + CGFloat(index) * (preferences.gapSize + preferences.dotRadius * 2),
y: rect.height / 2
),
radius: preferences.dotRadius,
startAngle: 0,
endAngle: .pi * 2,
clockwise: true)
if index == currentPage {
path.lineWidth = preferences.currentDotBorderWidth
preferences.color.set()
path.stroke()
} else {
preferences.color.withAlphaComponent(preferences.dimFactor).set()
path.fill()
}
}
}
}
extension PageControlView {
struct Preferences {
// MARK: Properties
var color: UIColor = .white
var dimFactor: CGFloat = 0.57
var dotRadius: CGFloat = 3.5
var gapSize: CGFloat = 5
var currentDotBorderWidth: CGFloat = 3
}
}