【问题标题】:How to remove UIPageViewController's Bounce effect in iOS for Swift 3/4如何在 iOS 中为 Swift 3/4 移除 UIPageViewController 弹跳效果
【发布时间】:2018-03-12 06:31:59
【问题描述】:

这是我的问题的截图:

【问题讨论】:

    标签: ios iphone swift uipageviewcontroller bounce


    【解决方案1】:

    首先在你的 UIPageViewController 中找到滚动视图并添加 UIScrollViewDelegate

    for view in self.pageViewController!.view.subviews {
            if let subView = view as? UIScrollView {
                subView.delegate = self
                subView.isScrollEnabled = true
                subView.bouncesZoom = false
    
            }
    }
    

    (在您的 ViewController 类中扩展 UIScrollViewDelegate)

    对于方法“scrollViewDidScroll”和“scrollViewWillEndDragging”上的 UIScrollViewDelegate,您可以添加类似这样的内容

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我已经像下面的代码那样做了。

      func scrollViewDidScroll(scrollView: UIScrollView) {
      if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
          scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
      } else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width {
          scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
          }
      }
      func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
          if currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width {
              scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
          } else if currentIndex == totalViewControllers - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width {
              scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0)
          }
      }
      

      【讨论】:

      • 感谢您的回答!
      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多