简体   繁体   中英

How to pass the UIView with a gesture recognizer as parameter?

I use:

  tap.addTarget(self, action: "handleTap:")
  self.view.addGestureRecognizer(tap)

I want to access to the view.tag property of view in handleTap method. So how do I pass the UIView which "tap" is in as parameter of handleTap?

Thanks.

func handleTap(sender:UITapGestureRecognizer) {
    if let tag = sender.view?.tag {
        println(tag)
    }
}

Try following code.

 override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.view.userInteractionEnabled = true
        var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        self.view.tag = 101
        self.view.addGestureRecognizer(tapGesture)
    }

    func handleTap(sender:UITapGestureRecognizer) {
        if let tag = sender.view?.tag {
            println(tag)
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM