ReportHeaderView.swift 4.97 KB
//
//  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)!
    }
    
}