当我单击月份的下一个按钮时,在月份和日期中都使用了下一个和上一个按钮时,应仅刷卡一个月而不是一天,并且与日期相同,请帮助我...我该如何使用此代码但是当我单击“下一个”或“上一个”按钮时,月份和日期都闪烁了,我想要一个人
@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)!
}
答案 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)
。