UIScrollView 不响应手势

UIScrollView doesn't respond to gestures

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

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

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

我的 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() 方法根本没有被调用,因为简单的打印在我的控制台中没有 return 任何东西。

我尝试在 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

问题在这里

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-关于尾随,只有最后一个视图尾随滚动视图的尾随固定

if index == 2 { // last 

 // make trailing with scrollView 
}

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

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
    } 
}

感谢您的帮助!