4thSpace
4thSpace

Reputation: 44312

How to pass additional parameter to UITapGestureRecognizer?

How do I pass a float value to the function handleTap? It seems like I have the parameter signatures setup correct but there isn't a way to pass the float value in.

let tapped = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:digit:)))
tapped.numberOfTapsRequired = 1
mycontrol.addGestureRecognizer(handleTap)


func handleTap(recognizer: UITapGestureRecognizer, aFloat: Float) {
    //do something here
}

Upvotes: 1

Views: 4758

Answers (1)

skJosh
skJosh

Reputation: 661

You can achieve this by creating your own TapGestureRecongnizer class which is the subclass of UITapGestureRecognizer. The example is shown below:

class MyTapGesture: UITapGestureRecognizer {
    var floatValue = 0.0
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let tapped = MyTapGesture.init(target: self, action: #selector(handleTap))
        tapped.floatValue = 5.0
        tapped.numberOfTapsRequired = 1
        view.addGestureRecognizer(tapped)
    }

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

    func handleTap(recognizer: MyTapGesture) {
        print(recognizer.floatValue)
    }
}

Upvotes: 16

Related Questions