【问题标题】:Gesture Recognizer does not work. The action of the element that is underneath is performed - SWIFT手势识别器不起作用。执行下方元素的操作 - SWIFT
【发布时间】:2023-03-16 03:50:02
【问题描述】:

手势识别器不起作用。

执行下面元素的动作。

我将 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

标签: swift xcode swift3 uiview uigesturerecognizer


【解决方案1】:

替换

speech.addGestureRecognizer(UIGestureRecognizer(target: tabBar, action: #selector(tapGesture)))

speech.addGestureRecognizer(UITapGestureRecognizer(target: tabBar, action: #selector(tapGesture)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多