Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding UIGestureRecognizer To Subview Programmatically in Swift

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

 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 UIAlert when it is pressed? What am I missing?

like image 633
Andrew Walz Avatar asked Feb 17 '16 20:02

Andrew Walz


2 Answers

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

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

}
like image 59
Raphael Silva Avatar answered Sep 19 '22 07:09

Raphael Silva


Swift 3 version code based on Raphael Silva answer:

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

}
like image 27
mriaz0011 Avatar answered Sep 18 '22 07:09

mriaz0011