测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年2月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✓ |
由Michal Jackowski维护。
依赖项 | |
NSDate-Escort | ~> 1.5 |
UIView+JMFrame | >= 0 |
要运行示例项目,请先克隆仓库,并在Example目录中运行pod install
。
MJCalendar可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod "MJCalendar"
MJConfiguration
类负责所有可自定义属性。要提交所有配置更改,请使用方法reloadView()
。
// Set displayed period type. Available types: Month, ThreeWeeks, TwoWeeks, OneWeek
self.calendarView.configuration.periodType = .Month
// Set shape of day view. Available types: Circle, Square
self.calendarView.configuration.dayViewType = .Circle
self.calendarView.reloadView()
键 | 类型 | 默认值 |
---|---|---|
periodType | enum [.Month, .ThreeWeeks, .TwoWeeks, .OneWeek] | .Month |
dayViewType | enum [.Square, .Circle] | .Circle |
startDayType | enum [.Monday, .Sunday] | .Monday |
selectedDayType | enum [.Filled, .Border] | .Border |
rowHeight | CGFloat | 30 |
dayViewSize | CGSize | CGSizeMake(24, 24) |
dayTextFont | UIFont | UIFont.systemFontOfSize(12) |
otherMonthBackgroundColor | UIColor | UIColor.clearColor() |
otherMonthDayViewBackgroundColor | UIColor | UIColor.clearColor() |
otherMonthTextColor | UIColor | UIColor.clearColor() |
dayBackgroundColor | UIColor | UIColor.clearColor() |
dayDayViewBackgroundColor | UIColor | UIColor.clearColor() |
dayTextColor | UIColor | UIColor.clearColor() |
selectedDayBackgroundColor | UIColor | UIColor.clearColor() |
selectedDayTextColor | UIColor | UIColor.clearColor() |
selectedBorderWidth | CGFloat | 1 |
weekLabelFont | UIFont | UIFont.systemFontOfSize(12) |
weekLabelTextColor | UIColor | UIColor.clearColor() |
weekLabelHeight | CGFloat | 25 |
minDate | NSDate? | nil |
maxDate | NSDate? | nil |
outOfRangeDayBackgroundColor | UIColor | UIColor.clearColor() |
outOfRangeDayTextColor | UIColor | UIColor.clearColor() |
selectDayOnPeriodChange | Bool | true |
除了在日历配置中设置颜色外,这些也可以在与代理方法一起设置。返回nil表示将显示配置中的颜色。
func calendar(calendarView: MJCalendarView, textColorForDate date: NSDate) -> UIColor? {
return self.dayColors[date]?.textColor
}
func calendar(calendarView: MJCalendarView, backgroundForDate date: NSDate) -> UIColor? {
return self.dayColors[date]?.backgroundColor
}
一旦显示的时间段被改变(无论是通过左划或右划还是通过选择日期方法),则会触发方法didChangePeriod。用法示例。
func calendar(calendarView: MJCalendarView, didChangePeriod periodDate: NSDate, bySwipe: Bool) {
// Sets month name according to presented dates
self.setTitleWithDate(periodDate)
// bySwipe diffrentiate changes made from swipes or select date method
if bySwipe {
// Scroll to relevant date in tableview
self.scrollTableViewToDate(periodDate)
}
}
当用户通过点击特定的一天来选择日期时,会触发方法didSelectDate。用法示例。
func calendar(calendarView: MJCalendarView, didSelectDate date: NSDate) {
self.scrollTableViewToDate(date)
}
从 MJConfiguration
中获取变量 minDate
和 maxDate
负责设置可选日期范围。定义 UI 时使用属性:outOfRangeDayBackgroundColor
和 outOfRangeDayTextColor
。
要使用代码选择日期,请使用 selectDate
方法。用法示例。
func scrollViewDidScroll(scrollView: UIScrollView) {
// Prevent changing selected day when non user scroll is triggered.
if !self.isScrollingAnimation {
// Get all visible cells from tableview
if let visibleCells = self.tableView.indexPathsForVisibleRows {
if let cellIndexPath = visibleCells.first {
// Get day by indexPath
let day = self.dateByIndex(cellIndexPath.row)
//Select day according to first visible cell in tableview
self.calendarView.selectDate(day)
}
}
}
}
要运行时更改显示的时间段,请使用 animateToPeriod
方法。方法参数:
Month
、ThreeWeeks
、TwoWeeks
、OneWeek
。如果类型与已显示的类型相同,则不会执行动画。self.calendarView.animateToPeriodType(period, duration: 0.2, animations: { (calendarHeight) -> Void in
// In animation block you can add your own animation. To adapat UI to new calendar height you can use calendarHeight param
self.calendarViewHeight.constant = calendarHeight
self.view.layoutIfNeeded()
}, completion: nil)
Xcode 7+、Swift 2.0+
Michał Jackowski,[email protected]
MJCalendar 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。