2016-11-02 113 views
0

我想用Xcode IB创建一个16栈式堆栈的可滚动组,位置一个在另一个之上,第17个固定堆栈顶部不滚动。我希望能够与可滚动的stackviews进行交互,所以我不希望他们在向下滚动后立即反弹回来。iOS:非弹跳式的栈顶堆栈顶部固定元素堆栈查看

任何编程要求我会在Swift中做。

目前我有:

  • 在视图的顶部的垂直stackview(Overview Stack View)含有
    • 一个水平stackview在概述堆栈视图的顶部,所述固定元件(此水平stackview包含2个文本字段)
    • 下面的scrollview,其中包含一个UIView,该UIView又包含16个水平的stackviews定位在Y轴上相距50个单位

我觉得,如果我用配置和BouncesBounces Vertically滚动视图在属性检查器,我可以滚动stackviews但他们总是反弹起来随即,使他们很难或不可能进行交互。如果我不包括BouncesBounces Vertically,那么stackviews组根本不会滚动。

Github上回购here

此图片显示在Xcode中的项目:

project in Xcode

我已经阅读了许多#2的问题和答案(这是我得到了这么远),但没有的建议解决方案帮助我解决了这个问题。

任何帮助将不胜感激!

回答

0

这适用于我的情况:

  1. 做的ViewController一个UIScrollView委托。
  2. 创建主滚动视图的出口。
  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. 
    } 
}