【发布时间】:2019-02-27 23:58:38
【问题描述】:
我在滚动视图中移动了所有 UIElement,以避免加载 UIViewcontroller。但是现在视图上的 GestureRecognizers 不再起作用了。
我的视图层次结构如下所示:
- UIViewController (SingleEventController)
- UIScrollView(事件滚动视图)
- UIView(内容视图)
- UILabel、UIView、UITableView 等
这看起来像是一个常见问题,因为我在 Stackoverflow 上发现了很多类似的问题: Example 1Example 2。 尽管如此,我还是没能解决我的案子..
EventScrollView 中的简化代码如下所示:
let locationLabel: UILabel = {
let label = UILabel()
label.text = "Standort"
label.isUserInteractionEnabled = true //Important!
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.isUserInteractionEnabled = true //not needed!
contentView.isUserInteractionEnabled = true //Important!
}
func setupViews() {
guard let parent = parentVC else { return }
let locationTap = UITapGestureRecognizer(target: parent, action: #selector(parent.openInGoogleMaps))
locationTap.cancelsTouchesInView = false //Important!
addSubview(contentView)
contentView.addSubview(locationLabel)
locationLabel.addGestureRecognizer(locationTap)
}
我错过了什么步骤?
顺便说一句,点击 contentView 内的UITableView 行也不会被注册。
【问题讨论】:
-
您的视图层次结构示例没有提及 UITableView 或其单元格?
-
我的意思是“等”。应该不重要,因为标签点击也没有注册。
-
好的,我只是想了解 tableView 在层次结构中的位置。听起来您有很多相互冲突的手势识别器,您可能需要指定哪些可以一起工作,哪些不能。我想长按您的 tableView 可能会起作用,但普通点击不会。见developer.apple.com/library/ios/documentation/uikit/reference/…
-
其实我只有这个手势识别器。长按也不行。
-
是的,但是 ScrollView 有平移和捏合识别器,tableView 有点击和平移识别器。
标签: ios swift uiview uiscrollview uigesturerecognizer