简体   繁体   English

UIScrollView不会将点击手势传播到其子级

[英]UIScrollView does not propagate tap gesture to its children

I am stuck on this and no other question on SO helped me... 我对此感到困惑,因此没有其他问题对我有所帮助...

I have view controller, which implements UIGestureRecognizerDelegate. 我有视图控制器,它实现了UIGestureRecognizerDelegate。 There are a few views, one of them is contentView, and inside that contentView, there is a UIScrollView. 有几个视图,其中一个是contentView,在该contentView中,有一个UIScrollView。 Also inside that scroll view, there is a scrollContentView, which is simple UIView, but it contains more subviews which I didn't include in code sample just because of space. 在该滚动视图中,还有一个scrollContentView,它是简单的UIView,但是它包含更多的子视图,由于空间原因,我没有将其包含在代码示例中。 Everything works fine (after a lot of time), but it looks like no tap gestures are propagated throw UIScrollView to the child view.. 一切正常(经过很多时间),但是似乎没有传播任何轻击手势,而是将UIScrollView扔到了子视图中。

I am not using storyboard. 我没有使用情节提要。

I tried everything, any help would be appreciated. 我尝试了一切,任何帮助将不胜感激。

class MyController: PopoverController, UITableViewDelegate, UIGestureRecognizerDelegate
{
    var scrollPane: UIScrollView!

    var myContentView: UIView!

    var bottomPane: EditOrderItemBottomPaneView!


    var quantityPaneView: QuantityPaneView!

    var optionsTable: OptionsTableView!

    var modificationsTable: ModificationsTableView!

    var specialPricingsTable: SpecialPricingsTableView!


    override func viewDidLoad()
    {
        super.viewDidLoad()

        categoriesModel = DIContainer.get().getCategoriesModel()
        activeOrderModel = DIContainer.get().getActiveOrderModel()
        modificationModel = DIContainer.get().getProductModificationsModel()

        orderItem = popoverModel.get("orderItem") as! OrderItem!
        let category: ProductCategory = categoriesModel.getCategoryById(orderItem.categoryId)!

        titleLabel.text = category.name

        // scroll view
        scrollPane = UIScrollView()
        scrollPane.translatesAutoresizingMaskIntoConstraints = false
        scrollPane.delegate = self
        contentView.addSubview(scrollPane)

        // bottom pane
        bottomPane = EditOrderItemBottomPaneView()
        contentView.addSubview(bottomPane)

        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView][bottomView]|", options: [], metrics: nil, views: ["scrollView": scrollPane, "bottomView": bottomPane]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollPane]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bottomView]|", options: [], metrics: nil, views: ["bottomView": bottomPane]))

        // scroll content view
        myContentView = UIView()
        myContentView.translatesAutoresizingMaskIntoConstraints = false
        scrollPane.addSubview(myContentView)
        scrollPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[content]|", options: [], metrics: nil, views: ["content": myContentView]))
        scrollPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[content]|", options: [], metrics: nil, views: ["content": myContentView]))

        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[content]|", options: [], metrics: nil, views: ["content": myContentView]))
        contentView.addConstraint(NSLayoutConstraint(item: myContentView, attribute: NSLayoutAttribute.Trailing,
                relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing,
                multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: myContentView, attribute: NSLayoutAttribute.Leading,
                relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading,
                multiplier: 1, constant: 0))

        // quantity pane
        quantityPaneView = QuantityPaneView()
        myContentView.addSubview(quantityPaneView)

        // options table
        let optionsDataSource: OrderItemOptionsTableViewDataSource = DIContainer.get().getOrderItemOptionsTableViewDataSource()
        optionsDataSource.orderItem = orderItem
        if optionsDataSource.getNumberOfOptions() > 0 {
            optionsTable = OptionsTableView(delegate: DIContainer.get().getOrderItemOptionsTableViewDelegate(),
                    dataSource: optionsDataSource, orderItem: orderItem)
            myContentView.addSubview(optionsTable)
        }

        // modifications table
        modificationsTable = ModificationsTableView(
        delegate: DIContainer.get().getOrderItemModificationsTableViewDelegate(),
                dataSource: DIContainer.get().getOrderItemModificationsTableViewDataSource(),
                orderItem: orderItem)
        myContentView.addSubview(modificationsTable)

        // special pricing table
        specialPricingsTable = SpecialPricingsTableView(
        delegate: DIContainer.get().getOrderItemSpecialPricingsTableViewDelegate(),
                dataSource: DIContainer.get().getOrderItemSpecialPricingsTableViewDataSource(),
                orderItem: orderItem)
        myContentView.addSubview(specialPricingsTable)

        var views = [
                "quantityPane": quantityPaneView,
                "modifications": modificationsTable,
                "specialPricings": specialPricingsTable
        ]

        myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[quantityPane]-25-|", options: [], metrics: nil, views: views))
        myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[modifications]-25-|", options: [], metrics: nil, views: views))
        myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[specialPricings]-25-|", options: [], metrics: nil, views: views))

        if optionsDataSource.getNumberOfOptions() > 0 {
            views["options"] = optionsTable
            myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-25-[options]-25-|", options: [], metrics: nil, views: views))
            myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[quantityPane]-25-[options]-25-[modifications]-25-[specialPricings]", options: [], metrics: nil, views: views))
        }
        else {
            myContentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[quantityPane]-25-[modifications]-25-[specialPricings]", options: [], metrics: nil, views: views))
        }
    }

    override func viewDidLayoutSubviews()
    {
        super.viewDidLayoutSubviews()

        var h: CGFloat = 0.0
        for view: UIView in myContentView.subviews {
            h += view.frame.size.height;
        }

        scrollPane.contentSize = CGSizeMake(myContentView.frame.size.width, h)
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
    {
        return true
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
    {
        if touch.view != scrollPane {
            return false
        }

        return true
    }
}

Implement UIGestureRecognizer delegate method. 实现UIGestureRecognizer委托方法。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view != yourScrollView) { 
        return NO;
    }
    return YES;
}

I realized that the whole screen was designed really badly. 我意识到整个屏幕的设计真的很糟糕。 I didn't solve the issue, but I reimplemented the whole controller. 我没有解决问题,但是我重新实现了整个控制器。 I still think that it is possible to solve that problem, but there is much easier way. 我仍然认为可以解决该问题,但是有更简单的方法。

The point is, that I had more table views inside that scroll content view. 关键是,在滚动内容视图中我有更多的表视图。 But of course, it is possible to create just one table with more sections. 但是,当然可以只创建一个包含更多部分的表。 Side effect of this would be that that one table view itself is scrollable and so there is no need for using custom UIScrollView. 这样做的副作用是,一个表视图本身是可滚动的,因此不需要使用自定义UIScrollView。

So for everyone beginning with iOS development, use UITableView with more sections wherever possible. 因此,对于从iOS开发开始的每个人,请尽可能在更多部分中使用UITableView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM