Blame view

HDFwear/Mine/MedalViewController.swift 7.16 KB
f2cf74c7   yangbin   1.0.20(4)
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
  //
  //  MedalViewController.swift
  //  HDFwear
  //
  //  Created by yangbin on 2022/3/23.
  //
  
  import UIKit
  import SnapKit
  
  class MedalViewController: UIViewController {
      
      @IBOutlet weak var collectionView: UICollectionView!
      @IBOutlet weak var collectionViewFlowLayout: UICollectionViewFlowLayout!
      var textArray: [String] = ["步数", "爬高", "热量", "里程", "跑步"]
  //    var medalArray: [[String]] = [["单日1万步", "单日2万步", "单日3万步", "累计5万步", "累计10万步", "累计50万步", "连续7天1万步", "连续21天1万步", "连续100天1万步"], ["胡夫金字塔", "埃菲尔铁塔", "广州塔", "迪拜塔", "富士山", "珠穆朗玛峰"], ["单日200千卡", "单日500千卡", "单日800千卡", "累计1000千卡", "累计5000千卡", "累计10000千卡", "累计2万千卡", "累计5万千卡", "累计10万千卡"], ["环操场20圈", "环操场50圈", "环操场100圈", "北京五环路", "青海湖", "塞浦路斯岛", "台湾岛", "爱尔兰岛", "马达加斯加岛"], ["挑战2千米", "挑战5千米", "挑战10千米", "挑战半马", "挑战全马", "累计10千米", "累计50千米", "累计100千米", "累计500千米", "累计1000千米", "连续7天跑步", "连续21天跑步"]]
  //    var valueDic: [String: String] = ["单日1万步"]
      
      var medalArray: [[MedalModel]] = []
      
      var language = AppSettings.shared.language
      
      override func viewDidLoad() {
          super.viewDidLoad()
          title = LocString("勋章")
                  
          for text in textArray {
              if let array = RealmTools.queryObjects(MedalModel.self, filter: "type == %@", [text]) as? [MedalModel] {
                  medalArray.append(array)
              }
          }
          
          print(medalArray)
          
  //        let plistPath = Bundle.main.path(forResource: "Medal", ofType: "plist")
  
          
          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)
          
          collectionViewFlowLayout.minimumLineSpacing = 1
          collectionViewFlowLayout.minimumInteritemSpacing = 10
          collectionViewFlowLayout.sectionInset = UIEdgeInsets(top: 2, left: 7, bottom: 5, right: 7)
          let width = (SCREEN_WIDTH-36)/3.0
          collectionViewFlowLayout.itemSize = CGSize(width: width, height: width+30)
          collectionViewFlowLayout.headerReferenceSize = CGSize(width: SCREEN_WIDTH, height: 30)
          collectionView.register(UINib.init(nibName: "MedalCell", bundle: .main), forCellWithReuseIdentifier: "MedalCell")
          collectionView.register(UINib(nibName: "MedalHeaderView", bundle: .main), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MedalHeaderView")
          collectionView.showsVerticalScrollIndicator = false
          // Do any additional setup after loading the view.
      }
      
      @objc func share() {
          collectionView.scrollToItem(at: IndexPath(row: 0, section: textArray.count-1), at: .bottom, animated: false)
          if let _ = 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)
              
          }
          //偏移量归零
          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
          //        }
          
      }
      
  
      /*
      // MARK: - Navigation
  
      // In a storyboard-based application, you will often want to do a little preparation before navigation
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          // Get the new view controller using segue.destination.
          // Pass the selected object to the new view controller.
      }
      */
  
  }
  
  extension MedalViewController: UICollectionViewDelegate, UICollectionViewDataSource {
      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
          var reusableView: UICollectionReusableView?
          if kind == UICollectionView.elementKindSectionHeader {
              let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MedalHeaderView", for: indexPath) as! MedalHeaderView
              headerView.titleLabel.text = LocString(textArray[indexPath.section])
              reusableView = headerView
          }
          return reusableView!
      }
      
      func numberOfSections(in collectionView: UICollectionView) -> Int {
          return textArray.count
      }
      
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return medalArray[section].count
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MedalCell", for: indexPath) as! MedalCell
          let medal = medalArray[indexPath.section][indexPath.row]
          var imageName = medal.title
          if medal.date == nil {
              imageName += "灰"
          }
          if language != .Chinese {
              imageName += "英"
          }
          cell.imageView.image = UIImage(named: imageName)
          cell.detailLabel.text = LocString(medal.title)
          return cell
      }
      
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  //        let cell = collectionView.cellForItem(at: indexPath) as! MedalCell
          let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Mine", identifier: "MedalDetailVC") as! MedalDetailVC
          let medal = medalArray[indexPath.section][indexPath.row]
          vc.medal = medal
          navigationController?.pushViewController(vc, animated: true)
      }
  }