CalendarView.swift 6.04 KB
//
//  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
    }
    */

}