【问题标题】:How to disable paging of UIPageViewController only for landscape orientation?如何仅针对横向禁用 UIPageViewController 的分页?
【发布时间】:2019-01-10 16:40:37
【问题描述】:

我有一个 scroll 过渡样式UIPageViewController,只有在设备处于横向时才需要禁用分页。但是分页应该是纵向的。

我在这里遇到过类似的问题,但不是我的具体需求。其中一些是:

How do I Disable the swipe gesture of UIPageViewController?

Disable Page scrolling in UIPageViewController

Disable/enable scrolling in UIPageViewController

Restrict UIPageViewController (with TransitionStyleScroll) pan gesture to a certain area

以上所有都指向完全禁用或限制平移手势到某个区域。

现在,如果我采取完全禁用的方法:

  • 我需要跟踪设备方向变化
  • 当方向设置为横向时禁用
  • 当方向更改为纵向时再次启用

如果我采取限制在某个区域的方法:

  • 我需要找到那个特定区域

  • 需要计算特定区域(在前一点中描述) 纵向和横向方向不同

  • 纵向的特定区域需要是 整个UIPageViewController 边界

  • 横向的特定区域需要是一个非常小的区域 (其框架可能是0011),其中用户将无法 执行平移操作。这个帧计算需要很 精确,因为我的UIPageViewController 占据了整个范围 横向主屏幕。

  • 然后可能需要再次跟踪不同设备的方向变化 特定区域

  • 的计算

作者建议了一些技术:

pvc.dataSource = nil // prevents paging

pvc.dataSource = `a valid dataSource object` // enables paging

所以,再次手动启用 + 禁用。跟踪方向变化和启用/禁用。

这对于我的特定用例并不安全,因为可能会多次分配数据源。


我认为还有其他方法无法修改以适应用例。

有没有捷径可以实现我的需要?

【问题讨论】:

    标签: ios swift uipageviewcontroller uiinterfaceorientation device-orientation


    【解决方案1】:

    回答我自己的问题,因为我已经实现了我需要的。

    子类化UIPageViewController 是最简单的方法。我们必须找到页面视图控制器用来处理其平移手势相关工作的底层UIScrollView。我们将在该内部滚动视图中添加另一个UIPanGestureRecognizer。此平移手势识别器本质上不会执行任何操作,但它会阻止内部平移手势识别器仅被识别为横向。

    示例实现:

    class CustomPageViewController: UIPageViewController, UIGestureRecognizerDelegate {
    
        override func viewDidLoad() {
            if let underlyingScrollView = view.subviews.compactMap({ $0 as? UIScrollView })
                                        .first {
    
                let pangestureRecognizer = UIPanGestureRecognizer()
                pangestureRecognizer.delegate = self
                underlyingScrollView.addGestureRecognizer(pangestureRecognizer)
                // at this point, the underlying scroll view will have two pan gesture
                // recognizer side by side. We have the control of our added pan gesture
                // recognizer through the delegate. We can conditionally recognize it or not
            }
        }
    
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, 
             shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) 
             -> Bool {
            // Returning true from here means, page view controller will behave as it is
            // Returning false means, paging will be blocked
            // As I needed to block paging only for landscape orientation, I'm just returning
            // if orientation is in portrait or not
            return UIApplication.shared.statusBarOrientation.isPortrait
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      相关资源
      最近更新 更多