如何将其他参数传递给UITapGestureRecognizer?

时间:2016-07-17 00:51:24

标签: ios swift2

如何将浮点值传递给函数handleTap?看起来我的参数签名设置正确,但是没有办法传递浮点值。

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
}

1 个答案:

答案 0 :(得分:5)

您可以通过创建自己的TapGestureRecongnizer类来实现这一目标,该类是UITapGestureRecognizer的子类。示例如下所示:

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