UIScrollView不响应手势

时间:2018-12-21 20:37:06

标签: ios swift uiscrollview ios-autolayout

我有一个UIScrollView,它基本上是一个仪表板,具有与用户相关的信息。它的下面有一个寻呼机,目前包含三个UIView。

我以前使用CGRect作为框架来设置不同的视图,将x偏移设置为UIScrollView的宽度*页码。效果很好,但是在滚动下面的UITableView时使UIScrollView消失是很麻烦的。

我现在使用带有约束的AutoLayout来显示信息,效果很好。但是,我的UIScrollView不再滚动了。它不响应我执行的任何滑动手势。但是,它确实响应了寻呼机中的点击,这向我显示了偏移量和约束是正确的,我只是无法滚动。看看:

Example

我的UIScrollView是这样组成的:

var dashboardView: UIScrollView = {

    let dashboardView = UIScrollView()
    dashboardView.translatesAutoresizingMaskIntoConstraints = false
    dashboardView.backgroundColor = UIColor.clear
    dashboardView.layer.masksToBounds = true
    dashboardView.layer.cornerRadius = 10
    dashboardView.isPagingEnabled = true
    dashboardView.showsHorizontalScrollIndicator = false
    dashboardView.showsVerticalScrollIndicator = false
    dashboardView.alwaysBounceHorizontal = false
    dashboardView.alwaysBounceVertical = false
    return dashboardView

}()

然后我像这样设置不同的视图:

for index in 0..<3 {

    let currentDash = UIView()
    currentDash.backgroundColor = UIColor.lightGray
    currentDash.layer.cornerRadius = 10
    currentDash.layer.masksToBounds = false
    currentDash.translatesAutoresizingMaskIntoConstraints = false
    currentDash.isUserInteractionEnabled = false
    dashboardView.addSubview(currentDash)
    currentDash.topAnchor.constraint(equalTo: dashboardView.topAnchor).isActive = true
    currentDash.bottomAnchor.constraint(equalTo: dashboardView.bottomAnchor).isActive = true
    currentDash.leadingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: dashboardWidth * CGFloat(index)).isActive = true
    currentDash.trailingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: (dashboardWidth * CGFloat(index)) + dashboardWidth).isActive = true
    currentDash.widthAnchor.constraint(equalToConstant: dashboardWidth).isActive = true
    currentDash.heightAnchor.constraint(equalToConstant: dashboardHeight).isActive = true

}

当我删除约束(AutoLayout)并使用框架对其进行设置时,它可以完美运行,如下所示:

dashFrame.origin.x = dashboardWidth * CGFloat(index)
dashFrame.size = CGSize(width: dashboardWidth, height: dashboardHeight)
let currentDash = UIView(frame: dashFrame)

但是,由于设置了框架,视图无法按我的意愿向上滚动,这就是为什么要使用AutoLayout的原因。

关于我在做什么错的任何想法或建议吗?我的ScrollViewDidScroll()方法只是不被调用,因为简单的打印不会在我的控制台中返回任何内容。

我尝试在currentDash UIView上将isUserInteractionEnabled属性设置为false,但这没有成功。我还尝试过分别删除所有约束,一起删除等等。但这也行不通-我至少需要一个topAnchor,bottomAnchor,heightAnchor和widthAnchor。

哦,当然,我设置了contentSize属性:

dashboardView.contentSize = CGSize(width: dashboardWidth * CGFloat(dashboardPager.numberOfPages), height: dashboardHeight)

任何对正确答案的建议或暗示将不胜感激。谢谢!

编辑:我还为UIScrollView本身设置了约束:

dashboardView.topAnchor.constraint(equalTo: dashboardBG.topAnchor).isActive = true
dashboardView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: normalSpacing).isActive = true
dashboardView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -normalSpacing).isActive = true
dashboardView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
dashboardView.heightAnchor.constraint(equalToConstant: dashboardHeight).isActive = true

dashboardView.delegate = self

2 个答案:

答案 0 :(得分:3)

问题在这里

currentDash.leadingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: dashboardWidth * CGFloat(index)).isActive = true
currentDash.trailingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: (dashboardWidth * CGFloat(index)) + dashboardWidth).isActive = true

1-关于视图的前导,第一个视图的前导应固定到scrollView,第二个视图的前导应固定到前一个视图的末尾,依此类推,如此

if index == 0{

   // make leading with scrolview leading
else
{

   // make leading with prevView trailing
}

因此,使一个以scrollView开头的var并更改for循环的结尾

2-关于尾部,只有最后一个视图尾部固定到scrollview的尾部

if index == 2 { // last 

 // make trailing with scrollView 
}

答案 1 :(得分:1)

由于Sh_Khan的回答,我终于弄明白了。我保留了原始代码,但删除了所有索引的railingAnchor,仅将其添加到最后一个索引。

for index in 0..<3 {

    let currentDash = UIView()
    currentDash.backgroundColor = UIColor.lightGray
    currentDash.layer.cornerRadius = 10
    currentDash.layer.masksToBounds = false
    currentDash.translatesAutoresizingMaskIntoConstraints = false
    currentDash.isUserInteractionEnabled = false
    dashboardView.addSubview(currentDash)
    currentDash.topAnchor.constraint(equalTo: dashboardView.topAnchor).isActive = true
    currentDash.bottomAnchor.constraint(equalTo: dashboardView.bottomAnchor).isActive = true
    currentDash.leadingAnchor.constraint(equalTo: dashboardView.leadingAnchor, constant: dashboardWidth * CGFloat(index)).isActive = true
    currentDash.widthAnchor.constraint(equalToConstant: dashboardWidth).isActive = true
    currentDash.heightAnchor.constraint(equalToConstant: dashboardHeight).isActive = true 
    if index == 2 {
        currentDash.trailingAnchor.constraint(equalTo: dashboardView.trailingAnchor).isActive = true
    } 
}

感谢您的帮助!