HealthReportVC.swift
7.77 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// HealthReportVC.swift
// Twear
//
// Created by yangbin on 2022/1/1.
//
import UIKit
import SwiftDate
class HealthReportVC: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet private weak var monthButton: UIButton!
@IBOutlet private weak var dayButton: UIButton!
@IBOutlet private weak var yearButton: UIButton!
@IBOutlet private weak var weekButton: UIButton!
private var dateType: DateType = .day
var collectArray = CurDevice.allFunctions
override func viewDidLoad() {
super.viewDidLoad()
title = LocString("健康报告")
// let shareButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
// shareButton.setImage(UIImage(named: "share_btn"), for: .normal)
// shareButton.addTarget(self, action: #selector(share), for: .touchUpInside)
// navigationItem.rightBarButtonItem = UIBarButtonItem(customView: shareButton)
if let i = collectArray.firstIndex(of: "MotionRecord") {
collectArray.remove(at: i)
}
if let i = collectArray.firstIndex(of: "WomenHealth") {
collectArray.remove(at: i)
}
if let i = collectArray.firstIndex(of: "Train") {
collectArray.remove(at: i)
}
if let i = collectArray.firstIndex(of: "Pressure") {
collectArray.remove(at: i)
}
if let i = collectArray.firstIndex(of: "Mett") {
collectArray.remove(at: i)
}
testDate()
collectArray.insert("Step", at: 0)
configureCollectionView()
}
func testDate() {
// let testDate = DateInRegion(year: 2022, month: 1, day: 13, hour: 0, minute: 0, second: 0)
if IsTest {
if let i = collectArray.firstIndex(of: "BloodPressure") {
collectArray.remove(at: i)
}
if let i = collectArray.firstIndex(of: "BloodOxygen") {
collectArray.remove(at: i)
}
}
}
@objc func share() {
collectionView.scrollToItem(at: IndexPath(row: collectArray.count-1, section: 0), at: .bottom, animated: false)
if let image = screenShot() {
let shareView = ShareView(self.screenShot())
shareView.show()
}
}
func screenShot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.collectionView.contentSize, true, 0.0)
//先保存原来frame 和 偏移量
let savedContentOffset = self.collectionView.contentOffset;
let savedFrame = self.collectionView.frame;
let contentSize = self.collectionView.contentSize;
let oldBounds = self.collectionView.layer.bounds;
if #available(iOS 13, *) {
//iOS 13 系统截屏需要改变tableview 的bounds
self.collectionView.layer.bounds = CGRect(x: oldBounds.origin.x, y: oldBounds.origin.y, width: contentSize.width, height: contentSize.height)
}
// let transY = NAVBAR_HEIGHT
// CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, transY);
// [colorLayer renderInContext:UIGraphicsGetCurrentContext()];
//偏移量归零
self.collectionView.contentOffset = CGPoint.zero;
//frame变为contentSize
self.collectionView.frame = CGRect(x: 0, y: 0, width: self.collectionView.contentSize.width, height: self.collectionView.contentSize.height)
//截图
self.collectionView.layer.render(in: UIGraphicsGetCurrentContext()!)
if #available(iOS 13,*) {
self.collectionView.layer.bounds = oldBounds
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//还原frame 和 偏移量
self.collectionView.contentOffset = savedContentOffset
self.collectionView.frame = savedFrame
return image
// }
}
func getShareImage() -> UIImage? {
collectionView.removeFromSuperview()
let image = collectionView.captureLongImage
view.addSubview(collectionView)
collectionView.snp.makeConstraints { (make) in
make.top.equalToSuperview().offset(50)
make.left.bottom.right.equalToSuperview()
}
return image
}
@IBAction func selectedDate(_ sender: UIButton) {
monthButton.setTitleColor(.black, for: .normal)
dayButton.setTitleColor(.black, for: .normal)
yearButton.setTitleColor(.black, for: .normal)
weekButton.setTitleColor(.black, for: .normal)
sender.setTitleColor(TintColor, for: .normal)
dateType = DateType(rawValue: sender.tag) ?? .day
collectionView.reloadData()
}
}
extension HealthReportVC: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
private func configureCollectionView(){
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.minimumLineSpacing = 12
flowLayout.minimumInteritemSpacing = 12
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 12.5, bottom: 15, right: 12.5)
flowLayout.itemSize = CGSize(width: SCREEN_WIDTH-25, height: 335)
collectionView.showsVerticalScrollIndicator = false
collectionView.bounces = false
collectionView.collectionViewLayout = flowLayout
collectionView.register(UINib(nibName: "StepReportCell", bundle: .main), forCellWithReuseIdentifier: "StepReportCell")
collectionView.register(UINib(nibName: "BOReportCell", bundle: .main), forCellWithReuseIdentifier: "BOReportCell")
collectionView.register(UINib(nibName: "HRReportCell", bundle: .main), forCellWithReuseIdentifier: "HRReportCell")
collectionView.register(UINib(nibName: "SleepReportCell", bundle: .main), forCellWithReuseIdentifier: "SleepReportCell")
collectionView.register(UINib(nibName: "BPReportCell", bundle: .main), forCellWithReuseIdentifier: "BPReportCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collectArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectArray[indexPath.row] {
case "BloodPressure":
let cell: BPReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BPReportCell", for: indexPath) as! BPReportCell
cell.dateType = dateType
return cell
case "BloodOxygen":
let cell: BOReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BOReportCell", for: indexPath) as! BOReportCell
cell.dateType = dateType
return cell
case "HeartRate":
let cell: HRReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HRReportCell", for: indexPath) as! HRReportCell
cell.dateType = dateType
return cell
case "Sleep":
let cell: SleepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SleepReportCell", for: indexPath) as! SleepReportCell
cell.dateType = dateType
return cell
case "Step":
let cell: StepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StepReportCell", for: indexPath) as! StepReportCell
cell.dateType = dateType
return cell
default:
let cell: StepReportCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StepReportCell", for: indexPath) as! StepReportCell
return cell
}
}
}