Pass Parameter with UITapGestureRecognizer

做~自己de王妃 提交于 2020-01-22 17:33:06

问题


Is there any way i can pass parameters with UITapGestureRecognizer? I've seen this answered for objective-c but couldn't find an answer for swift

test.userInteractionEnabled = true
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped4:"))
// Something like text.myParamater
test.addGestureRecognizer(tapRecognizer)

And then receive myParameter under func imageTapped4(){}


回答1:


One approach would be to subclass UITapGestureRecognizer and then set a property, I've posted an example below. You could also do some check on the sender and check if equal to some tag, class, string, e.t.c

class ViewController: UIViewController {

    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var image: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        image.userInteractionEnabled = true;
        let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:)))
        image.addGestureRecognizer(tappy)
        tappy.title = "val"
    }

    func tapped(sender : MyTapGesture) {
        print(sender.title)
        label1.text = sender.title
    }
}

class MyTapGesture: UITapGestureRecognizer {
    var title = String()
}

There are lots of examples on SO, have a look, good luck.




回答2:


For Swift 4

In Viewdidload

let label     =   UILabel(frame: CGRect(x: 0, y: h, width: Int(self.phoneNumberView.bounds.width), height: 30))
                label.textColor = primaryColor
                label.numberOfLines = 0
                label.font = title3Font
                label.lineBreakMode = .byWordWrapping
                label.attributedText = fullString

 let phoneCall = MyTapGesture(target: self, action: #selector(self.openCall))
        phoneCall.phoneNumber = "\(res)"
            label.isUserInteractionEnabled = true
            label.addGestureRecognizer(phoneCall)

Function as

@objc func openCall(sender : MyTapGesture) {
        let number = sender.phoneNumber
        print(number)
}

Write Class as

class MyTapGesture: UITapGestureRecognizer {
    var phoneNumber = String()
}

Follow Step Properly and make change according to your variable , button ,label . It WORKS PROPERLY




回答3:


The message passing in the tap gestures mostly like is by the location of the tap gesture detected in a view.

Suppose we have added a tap gesture in a view controller's self.view.

    override func viewDidLoad() {
    super.viewDidLoad()
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.printGesPostion))
    self.view.addGestureRecognizer(tapGesture)
    // Do any additional setup after loading the view, typically from a nib.
}

We can implement the printGesPosition method as

    func printGesPostion(ges:UITapGestureRecognizer) {
    print(ges.locationInView(self.view))

    let subView = self.view.hitTest(ges.locationInView(self.view), withEvent: nil)
    if subView?.isKindOfClass(UILabel) == true {

       print( (subView as! UILabel).text! )
    }
}

In this case, the method is detecting the position of the tap gesture, if it is happening on a UILabel, it will print the label.text

Don't forget set your label.userInteraction = true, either in ViewDidLoad or simply in the storyboard.




回答4:


The best way is to determind the parameter when the func imageTapped64is fired. You can get you params via the view (take a look to @Developer Sheldon answer) or in many other ways.



来源:https://stackoverflow.com/questions/38445262/pass-parameter-with-uitapgesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!