Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions MDatePickerView/ColDateCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +51,7 @@ class ColMonthCell: ColCell {
}
}
Col.scrollToItem(at: [0,(Selected[1]) - 1], at: .centeredHorizontally, animated: true)

}

override func selectDate(_ T : Int) {
Expand All @@ -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 {
Expand All @@ -74,6 +87,7 @@ class ColDayCell: ColCell {
Col.scrollToItem(at: [0,day - 1], at: .centeredHorizontally, animated: true)
return
}

Col.reloadData()
}
}
Expand Down
53 changes: 40 additions & 13 deletions MDatePickerView/MDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
])

}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}

}
6 changes: 5 additions & 1 deletion MDatePickerView/Utils/ColCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
}

Expand Down
4 changes: 2 additions & 2 deletions MDatePickerViewDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}()

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<a href="https://travis-ci.org/kciter/GlitchLabel"><img alt="Build Status" src="https://travis-ci.org/kciter/GlitchLabel.svg?branch=master"></a></p>
</div>

## Requirements
## Requirements
- iOS 9.0
- Xcode 9
- Swift 4.0
Expand All @@ -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
}()
```
Expand Down