swift4 - 当我们在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)!
}
解决方案
您可以使用此函数在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)
。
推荐阅读
- php - 如何使用 GET 作为表单的一部分从 Office UI 结构下拉列表中获取选定的值到 php
- javascript - 是否可以制作具有相等分区但每个分区中显示的数据不同的漏斗图(highcharts)?
- python - Django:使用 inlineformset_factory 更新和创建数据记录
- compilation - InvalidOperationException:未找到视图“Components/SomeName/Default”
- flutter - 我在 dio 库中遇到了 Flutter 的问题
- unity3d - 我是否需要在 google playstore 上发布我的应用程序才能集成 Unity Ads?
- reactjs - 枪不会实时更新
- swift - 如何在 Swift 中以与缩放、平移和旋转完全相同的方式将缩放、平移和旋转的图像从一个视图控制器添加到另一个视图控制器?
- python - 无法找到谷歌搜索结果的文本
- css - JavaFX CSS Next-sibling 选择器坏了?