diff --git a/MDatePickerView/ColDateCell.swift b/MDatePickerView/ColDateCell.swift index a2a67b7..7278ca0 100644 --- a/MDatePickerView/ColDateCell.swift +++ b/MDatePickerView/ColDateCell.swift @@ -9,13 +9,21 @@ import UIKit class ColMonthCell: ColCell { + let allMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] + + lazy var month : [String] = { + // for future uses + if self.to > 0 { + return Array(self.allMonths[...self.to]) + } else { + return self.allMonths + } + }() - let month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] var value = 0 - override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return 12 + return month.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { @@ -43,7 +51,7 @@ class ColMonthCell: ColCell { } } Col.scrollToItem(at: [0,(Selected[1]) - 1], at: .centeredHorizontally, animated: true) - + } override func selectDate(_ T : Int) { @@ -63,6 +71,11 @@ class ColDayCell: ColCell { let range = (calendar as NSCalendar?)?.range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: time.date ?? Date()) day = range?.length ?? 30 + // for future use + if self.to > 0 { + day = to + } + let week = DateComponents(calendar: Calendar.current, year: Selected[0], month: Selected[1],day: 1) let comps = (calendar as NSCalendar?)?.components(NSCalendar.Unit.weekday, from: week.date ?? Date()) if let Comps = comps?.weekday { @@ -74,6 +87,7 @@ class ColDayCell: ColCell { Col.scrollToItem(at: [0,day - 1], at: .centeredHorizontally, animated: true) return } + Col.reloadData() } } diff --git a/MDatePickerView/MDatePickerView.swift b/MDatePickerView/MDatePickerView.swift index 3c8ec63..a578f23 100644 --- a/MDatePickerView/MDatePickerView.swift +++ b/MDatePickerView/MDatePickerView.swift @@ -18,10 +18,12 @@ public class MDatePickerView : UIView { public var Color : UIColor = UIColor(red: 255/255, green: 97/255, blue: 82/255, alpha: 1) - public var from : Int = 1980 - - public var to : Int = Calendar.current.component(.year, from: Date()) +// public var from : Int = 1980 +// public var to : Int = Calendar.current.component(.year, from: Date()) + public var maxDate = Date() + public var minDate : Date = Date(timeIntervalSince1970: 0) + public var cornerRadius : CGFloat = 18 { didSet{ Col.layer.cornerRadius = cornerRadius @@ -30,7 +32,17 @@ public class MDatePickerView : UIView { public var selectDate : Date? { didSet{ - if let select = selectDate{ + if let selected = selectDate { + var select = selected + if select > maxDate { + select = maxDate + self.layoutSubviews() + } + if select < minDate { + select = minDate + self.layoutSubviews() + } + Y = Calendar.current.component(.year, from: select) D = Calendar.current.component(.day, from: select) M = Calendar.current.component(.month, from: select) @@ -74,10 +86,10 @@ public class MDatePickerView : UIView { addSubview(Col) NSLayoutConstraint.activate([ - Col.topAnchor.constraint(equalTo: topAnchor, constant: 0), - Col.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0), - Col.leftAnchor.constraint(equalTo: leftAnchor, constant: 0), - Col.rightAnchor.constraint(equalTo: rightAnchor, constant: 0) + Col.topAnchor.constraint(equalTo: topAnchor, constant: 0), + Col.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0), + Col.leftAnchor.constraint(equalTo: leftAnchor, constant: 0), + Col.rightAnchor.constraint(equalTo: rightAnchor, constant: 0) ]) } @@ -124,8 +136,8 @@ extension MDatePickerView : UICollectionViewDelegate,UICollectionViewDataSource, cell = Col.dequeueReusableCell(withReuseIdentifier: ColDayCellID, for: indexPath) as! ColDayCell case 2: cell = Col.dequeueReusableCell(withReuseIdentifier: ColYearCellID, for: indexPath) as! ColYearCell - cell?.from = from - cell?.to = to + cell?.from = Calendar.current.component(.year, from: minDate) + cell?.to = Calendar.current.component(.year, from: maxDate) default: break } @@ -158,13 +170,28 @@ extension MDatePickerView : ColCellDelegate { case .Year(select: let year, range: _): Y = year } - let dateComponents = DateComponents(calendar: Calendar.current, year: Y, month: M, day: D) if let date = dateComponents.date { - delegate?.mdatePickerView(selectDate: date) + var newDate = date + if newDate > maxDate { + newDate = maxDate + setToDate(date: newDate) + } + if newDate < minDate { + newDate = minDate + setToDate(date: newDate) + } + delegate?.mdatePickerView(selectDate: newDate) } - scrollToitem() + scrollToitem() + } + + private func setToDate(date: Date) { + Y = Calendar.current.component(.year, from: date) + D = Calendar.current.component(.day, from: date) + M = Calendar.current.component(.month, from: date) + self.layoutSubviews() } } diff --git a/MDatePickerView/Utils/ColCell.swift b/MDatePickerView/Utils/ColCell.swift index 64cbf85..92bc1af 100644 --- a/MDatePickerView/Utils/ColCell.swift +++ b/MDatePickerView/Utils/ColCell.swift @@ -125,9 +125,13 @@ extension ColCell : UICollectionViewDelegate,UICollectionViewDataSource,UICollec offset = CGPoint(x: roundedIndex * cellwidth - scrollView.contentInset.left, y: -scrollView.contentInset.top) targetContentOffset.pointee = offset - selectDate(Int(roundedIndex + 1)) + DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { + self.selectDate(Int(roundedIndex + 1)) + } + } + @objc func selectDate(_ T : Int) { } } diff --git a/MDatePickerViewDemo/ViewController.swift b/MDatePickerViewDemo/ViewController.swift index fff9fc5..fc4567c 100644 --- a/MDatePickerViewDemo/ViewController.swift +++ b/MDatePickerViewDemo/ViewController.swift @@ -17,8 +17,8 @@ class ViewController: UIViewController { mdate.Color = UIColor(red: 0/255, green: 178/255, blue: 113/255, alpha: 1) mdate.cornerRadius = 14 mdate.translatesAutoresizingMaskIntoConstraints = false - mdate.from = 1920 - mdate.to = 2050 + mdate.minDate = DateComponents(calendar: Calendar.current, year: 2018, month: 3).date! + mdate.maxDate = DateComponents(calendar: Calendar.current, year: 2020, month: 9).date! return mdate }() diff --git a/README.md b/README.md index 0b56cb9..cf605c9 100755 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Build Status

-## Requirements +## Requirements - iOS 9.0 - Xcode 9 - Swift 4.0 @@ -36,8 +36,8 @@ lazy var MDate : MDatePickerView = { let mdate = MDatePickerView() mdate.Color = .gray mdate.delegate = self - mdate.from = 1980 - mdate.to = 2100 + mdate.minDate = DateComponents(calendar: Calendar.current, year: 2018, month: 3).date! + mdate.maxDate = DateComponents(calendar: Calendar.current, year: 2020, month: 9).date! return mdate }() ```