-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialWormPaginationControl.swift
101 lines (87 loc) · 3.12 KB
/
MaterialWormPaginationControl.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
//
// MaterialWormPaginationControl.swift
// SBITO
//
// Created by Pisit W on 18/6/2563 BE.
// Copyright © 2563 Confusians. All rights reserved.
//
import UIKit
class MaterialWormPaginationControl: UIControl {
lazy var stackView: UIStackView = {
let sv = UIStackView()
sv.axis = .horizontal
sv.alignment = .fill
sv.distribution = .fill
sv.spacing = 8
sv.translatesAutoresizingMaskIntoConstraints = false
return sv
}()
override init(frame: CGRect) {
super.init(frame: frame)
shareInitialization()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
shareInitialization()
}
func shareInitialization() {
addSubview(stackView)
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 10).isActive = true
}
public var currentPage: Int = 0 {
didSet {
refreshImagesOfDots()
}
}
public var numberOfPages: Int = 0 {
didSet {
for (pageIndex, dotView) in stackView.arrangedSubviews.enumerated() {
dotView.removeFromSuperview()
}
for n in 0..<numberOfPages {
let view = DotView(frame: .init(x: 0, y: 0, width: 10, height: 10))
view.widthConstraint = view.widthAnchor.constraint(equalToConstant: 10)
view.widthConstraint?.isActive = true
stackView.addArrangedSubview(view)
}
refreshImagesOfDots()
}
}
var pageIndicatorTintColor: UIColor? {
didSet {
refreshImagesOfDots()
}
}
var currentPageIndicatorTintColor: UIColor? {
didSet {
refreshImagesOfDots()
}
}
func refreshImagesOfDots() {
customPageControl(dotFillColor: currentPageIndicatorTintColor ?? .white, notDotColor: pageIndicatorTintColor ?? .gray, dotBorderWidth: 1)
}
func customPageControl(dotFillColor:UIColor, notDotColor:UIColor, dotBorderWidth:CGFloat) {
layoutIfNeeded()
UIView.animate(withDuration: 0.3) {
for (pageIndex, dotView) in self.stackView.arrangedSubviews.enumerated() {
if self.currentPage == pageIndex {
dotView.backgroundColor = dotFillColor
dotView.widthConstaint?.constant = 32
dotView.layer.cornerRadius = dotView.frame.size.height / 2
}else{
dotView.backgroundColor = notDotColor
dotView.widthConstaint?.constant = 10
dotView.layer.cornerRadius = dotView.frame.size.height / 2
}
}
self.layoutIfNeeded()
}
}
}
class DotView: UIView {
var widthConstraint: NSLayoutConstraint?
}