EventsCalendar
Events Calendar是一个轻量级的iOS Swift库,可以帮助您轻松创建可定制的带有事件映射的日历UI。它支持同步和异步事件加载。它非常轻量且经过优化,可以轻松实现高达60 fps的渲染。实现平滑滚动并占用更少的内存。日历UI的属性可以完全自定义。
截图
用法
初始化
CalendarView可用两种格式:周格式和月格式。两种格式的使用方法相似。通过以下方式初始化一个MonthCalendarView:
lazy var monthCalendarView = {
let view = MonthCalendarView(
startDate: Date(fromFormattedString: "2000 01 01")!,
endDate: Date(fromFormattedString: "2050 12 01")!
)
view.allowsDateSelection = true // default value: true
view.selectedDate = Date()
view.isPagingEnabled = true // default value: true
view.scrollDirection = .horizontal // default value: .horizontal
view.viewConfiguration = CalendarConfig() // default valut: .default
return view
}()
WeekCalendarView同样可以按照这种方式初始化。
日历 UI 配置
日历的 UI 由 CalendarConfig 对象定义,该对象传递给 CalendarView 的 viewConfiguration 属性。CalendarConfig 包含超过 25 个可自定义的属性。这些属性包括:
backgroundColor: UIColor // background color of calendar
selectionColor: UIColor // selected date background color
dotColor: UIColor // event dot color
selectedDotColor: UIColor // event dot color of selected date
weekendLabelColor: UIColor // weekend date text color
validLabelColor: UIColor // valid date text color
invalidLabelColor: UIColor // invalid date text color
selectedLabelColor: UIColor // selected date text color
todayLabelColor: UIColor // today date text color
otherMonthLabelColor: UIColor // other month date text color
dateLabelFont: UIFont // font of date text
cellMaxWidth: CGFloat? // maximum width of each date cell
cellMaxHeight: CGFloat? // maximum height of each date cell
invalidatePastDates: Bool // shows past dates invalid date and unselectable
shouldConsiderSaturdayAsWeekend: Bool // considers saturday as weekend
monthTitleFont: UIFont // month title text font
monthTitleHeight: CGFloat // month title text height
monthTitleTextColor: UIColor // month title text color
monthTitleAlignment: NSTextAlignment // month title text alignment
monthTitleBackgroundColor: UIColor // month title background color
monthTitleStyle: MonthTitleStyle // month title style (short, full, ...)
monthTitleIncludesYear: Bool // displays year along with month title
weekdayTitles: [String] // week day titles
weekdayTitleFont: UIFont // week day title text font
weekdayTitleColor: UIColor // week day title text color
weekdayTitleHeight: CGFloat // week day title height
weekdayTitlesBackgroundColor: UIColor // week day title background color
CalendarViewDelegate
CalendarView 的回调将会调用到代理。使你的对象遵循 CalendarViewDelegate 协议,并将其分配给 CalendarView 的 delegate 属性。
monthCalendarView.delegate = self // self should conform to CalendarViewDelegate
每当日期选择发生变化时,都会调用 CalendarViewDelegate 的以下方法。
func calendarView(_ calendarView: CalendarProtocol, didChangeSelectionDateTo date: Date, at indexPath: IndexPath)
事件处理
EventsCalendar 以同步和异步方式处理事件。CalendarViewDelegate 有两个用于事件处理的可选方法。
func calendarView(_ calendarView: CalendarProtocol, eventDaysForCalendar type: CalendarViewType, with calendarInfo: CalendarInfo, and referenceDate: Date) -> Set<Int>?
func calendarView(_ calendarView: CalendarProtocol, eventDaysForCalendar type: CalendarViewType, with calendarInfo: CalendarInfo, and referenceDate: Date, completion: @escaping (Result<Set<Int>, Error>)->())
如果当前月的活动天数为已知,请使用第一种方法。
如果当前月的活动天数需要通过网络调用或其他重计算方式获取,请使用第二种方法。建议在不阻塞主线程的情况下执行重计算。
事件缓存
在同步方式添加事件时,默认启用 EventsCalendar 的事件缓存功能。这有助于减少不必要网络调用或重复操作,并提高日历中快速添加事件的能力。要关闭事件缓存,可以使用以下方式:
monthCalendarView.allowsEventsCaching = false // Default value: true
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
需求
支持iOS 9.0+
安装
EventsCalendar 可以通过 CocoaPods 获得。安装时,只需在您的 Podfile 中添加以下行:
pod 'EventsCalendar'
作者
Akaash Dev,[email protected]
许可证
EventsCalendar 遵循 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。