2个滚动视图之间的平滑过渡

时间:2018-10-05 12:43:18

标签: swift uiscrollview xlpagertabstrip

如下所示,我有一个ViewController(可将其称为viewController A),其中包含一个带有内部ViewController的容器视图(可将其称为ViewController B)。

因此,当您向上滚动ViewController A时,正在使用它的滚动视图,直到tabview到达顶部为止。一旦tabview到达顶部,ViewController B的滚动视图将接管并开始滚动容器View。然后,当您向下滚动时,viewController B的scrollview会一直显示到顶部,然后viewController A的scrollview接管该标题视图。

问题

所以滚动功能完全按照我想要的方式工作,但是我的问题是,当scrollViews在彼此之间切换时,它并不平滑。用户必须抬起屏幕的手指,然后将其放回原处,才能识别出正确的滚动视图。当用户向上或向下滑动滚动视图时,如何在彼此之间平滑切换而不必停下来时该如何做到?就像自动过渡一样。

我看过其他帖子,但是我没有设法提供任何帮助,因此我们将不胜感激。预先感谢

enter image description here enter image description here enter image description here

ViewController A

import Foundation
import UIKit
import XLPagerTabStrip

class viewControllerA: ButtonBarPagerTabStripViewController {

      @IBOutlet weak var containerHeightConstraint: NSLayoutConstraint!
      @IBOutlet weak var scrollView: UIScrollView! 
      @IBOutlet weak var contentView: UIView!
      @IBOutlet weak var headerView: HeaderView!
      @IBOutlet weak var tabView: ButtonBarView!    
      @IBOutlet weak var viewContainer: UIScrollView!

        override func viewDidLoad() {

       self.scrollView.delegate = self

           NotificationCenter.default.addObserver(self, selector: #selector(EnableScroll), name: NSNotification.Name(rawValue: "enableScroll"), object: nil)

            containerView.isScrollEnabled = false
            self.containerView.bounces = false

            super.viewDidLoad()
        }

    @objc func EnableScroll() {
        self.scrollView.isScrollEnabled = true
    }

        override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let yOffset = scrollView.contentOffset.y
        if scrollView == self.scrollView {
            if yOffset >= 250 {
                //tabView has reached the top
                //enable ViewController B's scrollViews
                 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EnableScroll"), object: nil)
            }
        }
    }
}

ViewController B

class viewControllerB: UIViewController, IndicatorInfoProvider, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!

     override func viewDidLoad() {
       super.viewDidLoad(

   self.scrollView.delegate = self

  NotificationCenter.default.addObserver(self, selector: #selector(EnableScroll), name: NSNotification.Name(rawValue: "EnableScroll"), object: nil)
        }

     @objc func EnableScroll() {        
        self.scrollView.isScrollEnabled = true       
            }

      Override func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let yOffset = scrollView.contentOffset.y
        if scrollView == self.scrollView {
            if yOffset <= 0 {
                //scrolling down, top of ViewControllerB is full in view
                self.scrollView.isScrollEnabled = false
                //enables ViewController A's scrollview
                 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "enableScroll"), object: nil)

            }
        }
    }
}

0 个答案:

没有答案