MJCalendar 0.4.0

MJCalendar 0.4.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年2月
SwiftSwift版本3.0
SPM支持SPM

Michal Jackowski维护。



 
依赖项
NSDate-Escort~> 1.5
UIView+JMFrame>= 0
 

  • 作者:
  • Michał Jackowski

MJCalendar

用法

要运行示例项目,请先克隆仓库,并在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 中获取变量 minDatemaxDate 负责设置可选日期范围。定义 UI 时使用属性:outOfRangeDayBackgroundColoroutOfRangeDayTextColor

公共方法

要使用代码选择日期,请使用 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 方法。方法参数:

  • periodType:可用类型:MonthThreeWeeksTwoWeeksOneWeek。如果类型与已显示的类型相同,则不会执行动画。
  • duration:动画持续时间(以秒为单位)
  • 动画块:可选块,用于在时间段动画期间添加自定义动画。用法示例。
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 文件。