正确解决新的快速点击动画模式/苹果新UIPageControl避免半页bug

问题描述

打开您的 iPhone 14+ 并在主屏幕上显示五个或更多页面。看UIPageControl

  1. 点击右侧。视图一次显示一页动画。

  2. 按住手指并在五个点之间旋转。它在五个屏幕之间切换。

  3. 现在将它放在点 0 上,然后向右点击 - 但要快速点击五次,即使用动画进行赛道。观察它有我相信 3 种不同的动画模式,最终(如果你走得非常快,在内部动画中)以一种时髦的匆忙赶上你的点击。

关于 (1) 这就是我们多年来一直编程的方式

@IBAction func userDidChangePageControl(_ sender: UIPageControl) {
    
    pageViewController.setViewControllers(
        [pageViewController.pages[newIndex]],direction: (pageViewController.currentIndex < newIndex) ? .forward : .reverse,animated: true,completion: nil)
}

关于 (2) 现在每个人都必须这样做

@IBAction func userDidChangePageControl(_ sender: UIPageControl) {
    
    let newIndex = sender.currentPage
    var animationIsNeeded: Bool = true
    
    if #available(iOS 14.0,*) {
        switch sender.interactionState {
            case .none:
                // WTF??
                break
            case .continuous:
                animationIsNeeded = false
            case .discrete:
                animationIsNeeded = true
            @unknown default:
                break
        }
    } else {
        // prior to iOS14
        animationIsNeeded = true
    }
    
    pageViewController.setViewControllers(
        [pageViewController.pages[newIndex]],animated: animationIsNeeded,completion: {})
}

然而这是完全错误的。尝试使用手指模式 (3) 并观察会发生什么。

我使用这个平庸的解决方案,

var animating: Bool = false

@IBAction func userDidChangePageControl(_ sender: UIPageControl) {
    let newIndex = sender.currentPage
    var animationIsNeeded: Bool = true
    if #available(iOS 14.0,*) {
        switch sender.interactionState {
            case .none:
                // WTF??
                break
            case .continuous:
                animationIsNeeded = false
            case .discrete:
                animationIsNeeded = true
            @unknown default:
                break
        }
    } else { animationIsNeeded = true } // ancient phones
    
    if animating { animationIsNeeded = false }
    if animationIsNeeded { animating = true }
    
    pageViewController.setViewControllers(
        [pageViewController.pages[newIndex]],completion: { [weak self] _ in
            guard let self = self else { return }
            self.animating = false
        })
}

这至少避免了错误,但与 Apple 的解决方案完全不同。

有谁知道如何实现标准的(见 iPhone 主屏幕)Apple 解决方案?

(此外,也许我遗漏了一些重要的东西,这些天有没有更直接的方法将 UIPageControl 绑定到页面视图控制器,它确实固有地处理了“新 Apple 方法 1,2,3”?)

在指法模式“3”中加入一个现在不正确的糟糕解决方案让我感到恶心。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...