xcode 使用 UITapGestureRecognizer 传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38445262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:03:28  来源:igfitidea点击:

Pass Parameter with UITapGestureRecognizer

iosiphonexcodeswift2xcode7

提问by Joe Cade

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

有什么办法可以用 UITapGestureRecognizer 传递参数吗?我已经看到这个回答了objective-c,但找不到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(){}

然后在func imageTapped4(){}下接收myParameter

回答by JingJingTao

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

一种方法是将 UITapGestureRecognizer 子类化,然后设置一个属性,我在下面发布了一个示例。您还可以对发件人进行一些检查,并检查是否等于某个标签、类、字符串等

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.

SO上有很多例子,看看吧,祝你好运。

回答by Sachin Rasane

For Swift 4

对于 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

正确执行步骤并根据您的变量、按钮、标签进行更改。它工作正常

回答by Mirko Brunner

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.

最好的方法是在 funcimageTapped64被触发时确定参数。您可以通过视图(查看@Developer Sheldon 的回答)或许多其他方式获取参数。