gpt4 book ai didi

ios - 创建无限滚动天数的 UIPageViewController

转载 作者:行者123 更新时间:2023-11-28 21:15:16 25 4
gpt4 key购买 nike

我的应用程序中有一个由 UIPageViewController 控制的日志。页面的 View Controller 工作正常,但 UIPageViewController 有一些不稳定的行为。首先,委托(delegate)方法(页面 View Controller 是它自己的委托(delegate)):

private func journalPage(for date: Date) -> JournalViewController? {
// Create a new view controller and pass suitable data.
guard let journalPage = storyboard?.instantiateViewController(withIdentifier: JournalViewController.storyboardID) as? JournalViewController else {
return nil
}

journalPage.date = date
return journalPage
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let yesterday = calendar.date(byAdding: .day, value: -1, to: self.date) else {
return nil
}

self.date = calendar.startOfDay(for: yesterday)
return journalPage(for: self.date)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

guard let tomorrow = calendar.date(byAdding: .day, value: 1, to: self.date) else {
return nil
}

self.date = calendar.startOfDay(for: tomorrow)
return journalPage(for: self.date)
}

我更新了 UIPageViewControllerdate,然后返回一个配置的日志页面。问题是经常有重复的页面(同一日期的页面),或者页面/日期被跳过。我找到了 this问题,但看不出我如何实现类似的东西。

我以前使用过 UIPageViewController 并且一直使用索引来解决这个问题,但在这种情况下索引没有意义,因为您可以滚动到时间的开始/结束(字面意思)。

如何确保在正确的时间制作新页面以避免重复/遗漏日期?

最佳答案

我在没有使用索引的情况下解决了这个问题(页面上的 date 属性就足够了)。这会在任一方向上无限滚动,而不会创建重复项或跳过页面。

extension JournalPageViewController: UIPageViewControllerDataSource {
/// Creates and returns a JournalViewController if it can be found in the storyboard,
/// and sets it's date to `date`
private func journalPage(for date: Date) -> JournalViewController? {
// Create a new view controller and pass suitable data.
guard let journalPage = storyboard?.instantiateViewController(withIdentifier: JournalViewController.storyboardID) as? JournalViewController else {
return nil
}

journalPage.date = date
return journalPage
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
debugPrint("Before: ", separator: "", terminator: "")
let today = (viewController as! JournalViewController).date
guard var yesterday = calendar.date(byAdding: .day, value: -1, to: today) else {
return nil
}
yesterday = calendar.startOfDay(for: yesterday)

return journalPage(for: yesterday)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
debugPrint("After: ", separator: "", terminator: "")
let today = (viewController as! JournalViewController).date
guard var tomorrow = calendar.date(byAdding: .day, value: 1, to: today) else {
return nil
}
tomorrow = calendar.startOfDay(for: tomorrow)

return journalPage(for: tomorrow)
}
}

extension JournalPageViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
self.date = (self.viewControllers?.first as! JournalViewController).date
}
}

关于ios - 创建无限滚动天数的 UIPageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com