iOS:堆栈视图堆栈的非弹跳滚动,顶部有固定元素

iOS: Non-bouncing scroll for stack of stackviews with stationary element at the top

我正在尝试使用 Xcode IB 创建一个由 16 个堆栈视图组成的可滚动组,一个位于另一个之上,顶部有一个不滚动的第 17 个固定堆栈视图。我希望能够与可滚动的堆栈视图进行交互,所以我不希望它们在我向下滚动后立即弹回。

我会在 Swift 中完成任何需要的编程。

目前我有:

我发现如果我在 Attributes Inspector 中使用 BouncesBounces Vertically 配置滚动视图,我可以滚动堆栈视图但它们总是立即弹回,使它们难以或无法交互和。如果我不包括 BouncesBounces Vertically,堆栈视图组根本不会滚动。

Github 回购 here

此图显示了 XCode 中的项目:

我已经在 Whosebug 上阅读了很多问题和答案(这是我走到这一步的方式),但是 none 所提出的解决方案帮助我解决了这个问题。

任何帮助将不胜感激!

这适用于我的情况:

  1. 使 ViewController 成为 UIScrollView 委托。
  2. 创建主滚动视图的出口。
  3. 添加基于的代码。 (我做了一些修改。)

请参阅下面 ViewController 的代码(也在 github 中)。

希望这对某人有所帮助!

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var mainScroll: UIScrollView!

    let stackHeight: CGFloat = 30.0

    override func viewDidLoad() {
        super.viewDidLoad()
        mainScroll.delegate = self
        print("----- viewDidLoad -----")
        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
        swipeDown.direction = UISwipeGestureRecognizerDirection.Down
        self.view.addGestureRecognizer(swipeDown)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
        swipeUp.direction = UISwipeGestureRecognizerDirection.Up
        self.view.addGestureRecognizer(swipeUp)
    }

    func swiped(gesture: UIGestureRecognizer)
    {
        if let swipeGesture =  gesture as? UISwipeGestureRecognizer
        {
            switch swipeGesture.direction
            {
            case UISwipeGestureRecognizerDirection.Down:
                print("DOWN")
                if (mainScroll.contentOffset.y >= stackHeight) {
                    mainScroll.contentOffset.y = mainScroll.contentOffset.y - stackHeight
                }
            case UISwipeGestureRecognizerDirection.Up:
                print("UP \(mainScroll.contentOffset.y)")
                if (mainScroll.contentOffset.y <= stackHeight*16 ) {
                    mainScroll.contentOffset.y = mainScroll.contentOffset.y+stackHeight
                }
            default:
                break
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}