ios 在 Swift 中以编程方式将 UIGestureRecognizer 添加到子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35466926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:32:34  来源:igfitidea点击:

Adding UIGestureRecognizer To Subview Programmatically in Swift

iosiphoneswiftuiviewuigesturerecognizer

提问by Andrew Walz

I currently have a subview that is created and added to the UIViewin ViewDidLoad(). I am attempting to user UIGestureRecognizersto detect a tap and unhide a particular button. My current code:

我目前有一个创建并添加到UIViewin的子视图ViewDidLoad()。我试图让用户UIGestureRecognizers检测点击并取消隐藏特定按钮。我目前的代码:

 override func viewDidLoad() {
    super.viewDidLoad()
    architectView = CustomClass(frame: self.view.bounds)
    self.view.addSubview(architectView)        
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
    gestureRecognizer.delegate = self
    architectView.addGestureRecognizer(gestureRecognizer)
}

func handleTap(gestureRecognizer: UIGestureRecognizer) {
    let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

The handleTap()function is a simple test to see if the taps are being recognized. This code does not trigger the UIAlertwhen it is pressed? What am I missing?

handleTap()功能是一个简单的测试,以查看是否可以识别点击。此代码UIAlert在按下时不会触发?我错过了什么?

回答by Raphael Silva

I tested your code here and it does work. However, I think you might be missing to add the UIGestureRecognizerDelegateprotocol to your View Controller. See below:

我在这里测试了你的代码,它确实有效。但是,我认为您可能没有将UIGestureRecognizerDelegate协议添加到您的视图控制器中。见下文:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    var architectView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        architectView = UIView(frame: self.view.bounds)
        self.view.addSubview(architectView)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
        gestureRecognizer.delegate = self
        architectView.addGestureRecognizer(gestureRecognizer)
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer) {
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

回答by mriaz0011

Swift 3 version code based on Raphael Silva answer:

基于 Raphael Silva 答案的 Swift 3 版本代码:

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    var architectView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        architectView = UIView(frame: self.view.bounds)
        self.view.addSubview(architectView)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(gestureRecognizer:)))
        gestureRecognizer.delegate = self
        architectView.addGestureRecognizer(gestureRecognizer)
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer) {
        let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}