CalendarView.swift
6.04 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// CalendarView.swift
// ParentAssistant
//
// Created by Cao yang on 2018/3/27.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
class CalendarView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
///通知父控制器解除滑动限制
typealias CloseWindowBlock = ()->()
var keyWindow = UIWindow()
var backBtn = UIButton()
var topBtn = UIButton()
var weekLab = ["日","一","二","三","四","五","六"]
var firstDay = Int()
var monthDay = Int()
var todayDay = Int()
var viewWidth = CGFloat()
var viewHeight = CGFloat()
var backBlock : CloseWindowBlock?
override init(frame: CGRect) {
super.init(frame: frame)
if !frame.isEmpty{
viewWidth = frame.width
viewWidth = frame.height
self.backgroundColor = UIColor.orange
self.addSubview(self.collectionView)
collectionView.snp.makeConstraints { (maker) in
maker.center.equalToSuperview()
maker.height.width.equalToSuperview()
}
firstDay = CalendarDateManager.shared.getFirstMonthDays()
monthDay = CalendarDateManager.shared.getCurrentMonthDay()
todayDay = CalendarDateManager.shared.getTodayDate()
}
}
override func layoutSubviews() {
self.flowlayOut.itemSize = CGSize.init(width: self.bounds.size.width/7, height: 35)
viewWidth = self.bounds.size.width
viewHeight = self.bounds.size.height
}
lazy var flowlayOut = {()-> UICollectionViewFlowLayout in
//设置layout
let flowlayout = UICollectionViewFlowLayout()
flowlayout.itemSize = CGSize.init(width: viewWidth/7, height:35)
flowlayout.minimumLineSpacing = 0
flowlayout.minimumInteritemSpacing = 0
flowlayout.headerReferenceSize = CGSize.init(width: 0, height: 0)
flowlayout.footerReferenceSize = CGSize.init(width: 0, height: 0)
flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
return flowlayout
}()
lazy var collectionView = {()-> UICollectionView in
let calendarView = UICollectionView.init(frame: self.bounds, collectionViewLayout: self.flowlayOut)
calendarView.delegate = self
calendarView.dataSource = self
calendarView.backgroundColor = UIColor.white
calendarView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "calendarCell")
return calendarView
}()
//MARK: - 展示视图
func showView(){
let key = UIApplication.shared.keyWindow!
let top = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: viewWidth, height: statusBarHeight+navigationBarHeight!))
top.backgroundColor = UIColor.clear
top.addTarget(self, action: #selector(closeView), for: UIControlEvents.allEvents)
let back = UIButton.init(frame: CGRect.init(x: 0, y: viewHeight+statusBarHeight+navigationBarHeight!, width: viewWidth, height: screenHeight*2))
back.backgroundColor = UIColor.black
back.alpha = 0.4
back.addTarget(self, action: #selector(closeView), for: UIControlEvents.allEvents)
key.addSubview(top)
key.addSubview(back)
top.snp.makeConstraints { (maker) in
maker.width.equalToSuperview()
maker.height.equalTo(statusBarHeight+navigationBarHeight!)
maker.top.equalToSuperview()
}
back.snp.makeConstraints { (maker) in
maker.width.equalToSuperview()
maker.height.equalToSuperview()
maker.top.equalTo(viewHeight+statusBarHeight+navigationBarHeight!)
}
self.isHidden = false
keyWindow = key
backBtn = back
topBtn = top
}
//MARK: - 关闭视图
@objc private func closeView() {
self.backBlock!()
self.backBtn.removeFromSuperview()
self.topBtn.removeFromSuperview()
self.keyWindow = UIWindow.init()
self.isHidden = true
}
//MARK: - Delegate
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return weekLab.count + monthDay + firstDay - 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calendarCell", for: indexPath)
let lab = UILabel.init(frame: cell.bounds)
if indexPath.row < weekLab.count {
lab.text = weekLab[indexPath.row]
}else{
// 超出这个月显示为空
if indexPath.row+1 >= firstDay+7 && indexPath.row < monthDay+7+firstDay {
lab.text = "\(indexPath.row+1-firstDay-7+1)"
}else{
lab.text = ""
}
}
// 设置日期颜色
if indexPath.row+1 > todayDay+7+firstDay-1{
lab.textColor = UIColor.gray
}else
if indexPath.row+1 == todayDay+7+firstDay-1 {
lab.textColor = UIColor.red
}else{
lab.textColor = UIColor.black
}
lab.textAlignment = .center
cell.addSubview(lab)
lab.snp.makeConstraints { (maker) in
maker.center.equalToSuperview()
maker.height.width.equalToSuperview()
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row >= firstDay-1+7 && indexPath.row < todayDay+7+firstDay-1 {
DebugLog( "点击了 ...\(indexPath.row+1-7-firstDay+1)号")
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}