-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKSnapView.swift
More file actions
152 lines (113 loc) · 4.51 KB
/
KSnapView.swift
File metadata and controls
152 lines (113 loc) · 4.51 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
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
//
// KSnapView
//
// Copyright © 2017 Kenan Atmaca. All rights reserved.
// kenanatmaca.com
//
//
import UIKit
protocol KSnapViewDelegate:class {
func didChange(index:Int)
func finished(flag:Bool)
}
class KSnapView {
private var contentViews:[UIView] = []
private var rootView:UIView!
private var contentStack:UIStackView!
private var counter:Int = 0
private var autoTimer:Timer!
var delegate:KSnapViewDelegate?
var count:Int = 1
var duration:Double = 5.0
init(to: UIViewController) {
self.rootView = to.view
}
func setup() {
contentViews = []
for _ in 0..<count {
let contentView = UIView()
contentView.frame = CGRect(x: 0, y: 0, width: (rootView.frame.width - 30.0) / CGFloat(count), height: 8)
contentView.backgroundColor = UIColor.white.withAlphaComponent(0.4)
contentView.layer.cornerRadius = 4
let contentSubLayer = CALayer()
contentSubLayer.frame = contentView.frame
contentSubLayer.cornerRadius = 4
contentSubLayer.backgroundColor = UIColor.white.cgColor
contentSubLayer.frame.size.width = 0
contentSubLayer.anchorPoint = CGPoint(x: 0, y: contentView.layer.anchorPoint.y)
contentView.layer.addSublayer(contentSubLayer)
contentViews.append(contentView)
}
contentStack = UIStackView(arrangedSubviews: contentViews)
contentStack.alignment = .fill
contentStack.distribution = .fillEqually
contentStack.spacing = 3.0
contentStack.axis = .horizontal
rootView.addSubview(contentStack)
contentStack.translatesAutoresizingMaskIntoConstraints = false
contentStack.widthAnchor.constraint(equalTo: rootView.widthAnchor, multiplier: 0.93).isActive = true
contentStack.heightAnchor.constraint(equalToConstant: 8).isActive = true
contentStack.topAnchor.constraint(equalTo: rootView.topAnchor, constant: 20).isActive = true
contentStack.centerXAnchor.constraint(equalTo: rootView.centerXAnchor, constant: 0).isActive = true
autoNext()
autoTimer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(autoNext), userInfo: nil, repeats: true)
}
private func setAnimation(index:Int) {
contentViews.forEach { (view) in
view.layer.sublayers?.forEach({ (layer) in
layer.removeAllAnimations()
})
}
let anim = CABasicAnimation(keyPath: "bounds.size.width")
anim.duration = duration
anim.fromValue = 0
anim.toValue = contentViews[0].frame.width
anim.isRemovedOnCompletion = false
anim.fillMode = kCAFillModeForwards
contentViews[index].layer.sublayers?.first?.add(anim, forKey: nil)
}
@objc func autoNext() {
if counter < count {
delegate?.finished(flag: false)
delegate?.didChange(index: counter)
setAnimation(index: counter)
} else {
counter = -1
prevNext()
delegate?.finished(flag: true)
}
counter += 1
}
private func prevNext() {
counter += 1
autoTimer.invalidate()
if counter != count {
delegate?.finished(flag: false)
delegate?.didChange(index: counter)
setAnimation(index: counter)
autoTimer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(autoNext), userInfo: nil, repeats: true)
} else {
delegate?.finished(flag: true)
counter = -1
}
}
func next() {
autoTimer.invalidate()
if counter != count {
delegate?.finished(flag: false)
delegate?.didChange(index: counter)
setAnimation(index: counter)
autoTimer = Timer.scheduledTimer(timeInterval: duration, target: self, selector: #selector(autoNext), userInfo: nil, repeats: true)
} else {
delegate?.finished(flag: true)
counter = -1
}
counter += 1
}
func remove() {
counter = 0
autoTimer.invalidate()
contentStack.removeFromSuperview()
contentViews.removeAll()
}
}//