当我们在swift4中单击fscalendar中的下一个或上一个按钮时,如何只刷一个月或一天?

时间:2018-09-13 11:42:10

标签: swift4

当我单击月份的下一个按钮时,在月份和日期中都使用了下一个和上一个按钮时,应仅刷卡一个月而不是一天,并且与日期相同,请帮助我...我该如何使用此代码但是当我单击“下一个”或“上一个”按钮时,月份和日期都闪烁了,我想要一个人

  @IBAction func year_leftbtnAction(_ sender: Any)
{
    print("Clicked")

    calendar.setCurrentPage(getPreviousMonth(date: calendar.currentPage), animated: true)
}

func getPreviousMonth(date:Date)->Date
{
    return  Calendar.current.date(byAdding: .weekOfMonth, value: -1, to:date)!
}


@IBAction func year_rightbtnAction(_ sender: Any)
{
    print("Clicked")

    calendar.setCurrentPage(getNextMonth(date: calendar.currentPage), animated: true)
}

func getNextMonth(date:Date)->Date
{
    return  Calendar.current.date(byAdding: .weekOfMonth, value: 1, to:date)!
}

1 个答案:

答案 0 :(得分:1)

您可以使用此功能切换FSCalendar 2.8.0中的任何日期组件(使用Swift 4.2 / Xcode 10):

func switchDateComponent(component: Calendar.Component, isNextDirection: Bool) {
    if let nextDate = Calendar.current.date(byAdding: component, value: isNextDirection ? 1 : -1, to: calendar.selectedDate ?? Date()) {
        calendar.select(nextDate, scrollToDate: true)
    }
}

现在,如果您需要选择前一天/后一天,可以致电switchDateComponent(component: .day, isNextDirection: false)switchDateComponent(component: .day, isNextDirection: true)。对于月份部分,这将是相似的,只需调用switchDateComponent(component: .month, isNextDirection: false)switchDateComponent(component: .month, isNextDirection: true)