首页 > 解决方案 > 当我们在swift4中单击fscalendar中的下一个或上一个按钮时,我们如何只滑动一个月或一天

问题描述

当我在月份和日期都使用下一个和上一个按钮时,当我单击月份下一个按钮时,它应该只滑动月份而不是日期,并且与日期相同,请帮助我......我该怎么做我正在使用此代码但是当我单击下一个或上一个按钮,月份和日期都在滑动我想要个人

  @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)!
}

标签: swift4

解决方案


您可以使用此函数在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)or switchDateComponent(component: .month, isNextDirection: true)


推荐阅读