[英]UIScrollView does not propagate tap gesture to its children
我对此感到困惑,因此没有其他问题对我有所帮助...
我有视图控制器,它实现了UIGestureRecognizerDelegate。 有几个视图,其中一个是contentView,在该contentView中,有一个UIScrollView。 在该滚动视图中,还有一个scrollContentView,它是简单的UIView,但是它包含更多的子视图,由于空间原因,我没有将其包含在代码示例中。 一切正常(经过很多时间),但是似乎没有传播任何轻击手势,而是将UIScrollView扔到了子视图中。
我没有使用情节提要。
我尝试了一切,任何帮助将不胜感激。
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
}
}
实现UIGestureRecognizer委托方法。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view != yourScrollView) {
return NO;
}
return YES;
}
我意识到整个屏幕的设计真的很糟糕。 我没有解决问题,但是我重新实现了整个控制器。 我仍然认为可以解决该问题,但是有更简单的方法。
关键是,在滚动内容视图中我有更多的表视图。 但是,当然可以只创建一个包含更多部分的表。 这样做的副作用是,一个表视图本身是可滚动的,因此不需要使用自定义UIScrollView。
因此,对于从iOS开发开始的每个人,请尽可能在更多部分中使用UITableView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.