手势识别器不起作用。执行下方元素的操作 - SWIFT
Posted
技术标签:
【中文标题】手势识别器不起作用。执行下方元素的操作 - SWIFT【英文标题】:Gesture Recognizer does not work. The action of the element that is underneath is performed - SWIFT 【发布时间】:2019-07-16 09:55:11 【问题描述】:手势识别器不起作用。
执行下面元素的动作。
我将 UIView 添加到 TabBarController。
如果我点击创建的 UIView,它下面的元素的动作就会被执行。
class func createSpeechView(tabBar: UITabBarController)
let speech = Bundle.main.loadNibNamed("Speech", owner: Bundle.main, options: nil)![0] as! SpeechView
tabBar.tabBar.addSubview(speech)
speech.translatesAutoresizingMaskIntoConstraints = false
speech.centerXAnchor.constraint(equalTo: tabBar.tabBar.centerXAnchor).isActive = true
speech.topAnchor.constraint(equalTo: tabBar.tabBar.topAnchor, constant: -(height + 10)).isActive = true
speech.widthAnchor.constraint(equalTo: tabBar.tabBar.widthAnchor, multiplier: 1.0, constant: -30).isActive = true
speech.heightAnchor.constraint(equalToConstant: height).isActive = true
speech.isUserInteractionEnabled = true
speech.addGestureRecognizer(UIGestureRecognizer(target: tabBar, action: #selector(tapGesture)))
@objc class func tapGesture()
print("tap")
例子:
【问题讨论】:
使用 UITapGestureRecognizer 【参考方案1】:替换
speech.addGestureRecognizer(UIGestureRecognizer(target: tabBar, action: #selector(tapGesture)))
与
speech.addGestureRecognizer(UITapGestureRecognizer(target: tabBar, action: #selector(tapGesture)))
【讨论】:
以上是关于手势识别器不起作用。执行下方元素的操作 - SWIFT的主要内容,如果未能解决你的问题,请参考以下文章