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

时间:2016-02-17 20:20:44

标签: ios iphone swift uiview uigesturerecognizer

我目前有一个创建的子视图,并将其添加到ViewDidLoad()中的UIView中。我正在尝试使用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)
}

handleTap()函数是一个简单的测试,用于查看是否正在识别抽头。按下时,此代码不会触发UIAlert?我错过了什么?

2 个答案:

答案 0 :(得分:12)

我在这里测试了你的代码,它确实有效。但是,我认为您可能会遗漏将UIGestureRecognizerDelegate协议添加到View Controller中。见下文:

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.
    }

}

答案 1 :(得分:10)

基于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.
    }

}