ReportHeaderView.swift
4.97 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
//
// ReportHeaderView.swift
// ParentAssistant
//
// Created by Cao yang on 2018/4/14.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
class ReportHeaderView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
lazy var collectionView = {()-> UICollectionView in
let flowlayout = UICollectionViewFlowLayout()
flowlayout.itemSize = CGSize.init(width: screenWidth/7, height:60)
flowlayout.minimumLineSpacing = 0
flowlayout.minimumInteritemSpacing = 0
flowlayout.scrollDirection = UICollectionViewScrollDirection.horizontal
flowlayout.headerReferenceSize = CGSize.init(width: 0, height: 0)
flowlayout.footerReferenceSize = CGSize.init(width: 0, height: 0)
flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
let collect = UICollectionView.init(frame: CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height), collectionViewLayout: flowlayout)
collect.delegate = self
collect.dataSource = self
collect.showsHorizontalScrollIndicator = true
collect.scrollsToTop = false
collect.showsVerticalScrollIndicator = false
collect.isPagingEnabled = true
collect.backgroundColor = UIColor.white
collect.register(UINib.init(nibName: "ReportHeaderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ReportHeaderCollectionViewCell")
return collect
}()
//Data
/// 展示
var count = Int()
/// 当月1号 周几
var firstDay = Int()
/// 当月天数
var monthDays = Int()
/// 今天日期
var todayDate = Int()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(self.collectionView)
self.count = CalendarDateManager.shared.getMonthDayCount()*7
self.firstDay = CalendarDateManager.shared.getFirstMonthDays()
self.monthDays = CalendarDateManager.shared.getCurrentMonthDay()
self.todayDate = CalendarDateManager.shared.getTodayDate()
//初始位置:显示本周
collectionView.scrollToItem(at: [0,count-1], at: UICollectionViewScrollPosition.right, animated: true)
}
//MARK: - Collection Delegate && DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReportHeaderCollectionViewCell", for: indexPath) as! ReportHeaderCollectionViewCell
cell.setCellData(index: indexPath)
// 超出这个月显示为空
if indexPath.row+1 >= firstDay && indexPath.row <= monthDays {
cell.dataLab.text = "\(indexPath.row+1-firstDay+1)"
}else{
cell.dataLab.text = ""
}
// 设置日期颜色
if indexPath.row+1 > todayDate+firstDay-1{
cell.dataLab.textColor = UIColor.gray
}else
if indexPath.row+1 == todayDate+firstDay-1 {
cell.dataLab.textColor = UIColor.red
}else{
cell.dataLab.textColor = UIColor.black
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row >= firstDay-1 && indexPath.row < todayDate+firstDay-1 {
print("点击了 ...\(indexPath.row+1-firstDay+1)号")
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class CalendarDateManager: NSObject {
static let shared = CalendarDateManager()
private var date = Date.init(timeInterval: 150*86400, since: Date())
// private var date = Date()
override init() {}
/// 获取当前周ofMonth
///
/// - Returns: 第几周
func getMonthDayCount() -> Int {
let comps = Calendar.current.dateComponents([.weekOfMonth], from: date)
return comps.weekOfMonth!
}
/// 获取当前月第一天周几
///
/// - Returns: 月1号周几
func getFirstMonthDays() -> Int{
let comps = Calendar.current.dateComponents([.day], from: date)
let dayDate = comps.day!-1
let firstDay = Date.init(timeInterval: TimeInterval(-dayDate*86400), since: date)
let firstMonth = Calendar.current.dateComponents([.weekday], from: firstDay)
return firstMonth.weekday!
}
/// 获取当天
///
/// - Returns: 今天几号
func getTodayDate() -> Int {
let comps = Calendar.current.dateComponents([.day], from: date)
return comps.day!
}
/// 获取当月天数
///
/// - Returns: 天数
func getCurrentMonthDay () ->Int{
let comps = Calendar.current.range(of: .day, in: .month, for: date)
return (comps?.count)!
}
}