【问题标题】:iOS: Non-bouncing scroll for stack of stackviews with stationary element at the topiOS:堆栈视图堆栈的非弹跳滚动,顶部有固定元素
【发布时间】:2017-03-15 05:04:49
【问题描述】:

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

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

目前我有:

  • 视图顶部的垂直堆栈视图 (Overview Stack View) 包含
    • 概览堆栈视图顶部的一个水平堆栈视图作为固定元素(此水平堆栈视图包含 2 个文本字段)
    • 其下方的滚动视图,其中包含一个 UIView,该 UIView 又包含 16 个水平堆栈视图,在 Y 轴上相距 50 个单位

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

Github 仓库here

这张图片显示了 XCode 中的项目:

我已经阅读了有关 Stackoverflow 的许多问题和答案(这就是我走到这一步的原因),但没有一个建议的解决方案能帮助我解决这个问题。

任何帮助将不胜感激!

【问题讨论】:

    标签: ios xcode uiscrollview interface-builder uistackview


    【解决方案1】:

    这适用于我的情况:

    1. 使 ViewController 成为 UIScrollView 委托。
    2. 为 Main ScrollView 创建一个出口。
    3. 根据this answer添加代码。 (我做了一些更改。)

    查看下面的 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.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      相关资源
      最近更新 更多