简体   繁体   中英

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. There are a few views, one of them is contentView, and inside that contentView, there is a 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. Everything works fine (after a lot of time), but it looks like no tap gestures are propagated throw UIScrollView to the child view..

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.

- (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.

So for everyone beginning with iOS development, use UITableView with more sections wherever possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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