我有很多以编程方式创建的Button,它们可以随时更改。我的轻按手势:

let apriVideoGesture = UITapGestureRecognizer(target: self, action: #selector(PrincipaleController.apriVideo(_:)))
cont.addGestureRecognizer(apriVideoGesture)

func apriVideo(sender : UITapGestureRecognizer){

}

如何传递参数?像这样的东西:
let apriVideoGesture = UITapGestureRecognizer(target: self, action: #selector(PrincipaleController.apriVideo(stringa : "ciao")))
cont.addGestureRecognizer(apriVideoGesture)

func apriVideo(sender : UITapGestureRecognizer, stringa : String){

}

对不起,英语不好,我是意大利人

最佳答案

首先,如果您正在使用按钮,那么为什么要添加点击手势?您可以将目标添加为

btn.addTarget(self, action: #selector(self.btnPressed(_:)), forControlEvents: .TouchDragInside)

,但是仍然可以通过将手势用作来实现您的目标

坚持使用UIView
class ViewController: UIViewController {

let arrayOfSongsURL : [String] = [String]()
let startingTag = 100
override func viewDidLoad() {
    super.viewDidLoad()
    let height : CGFloat = 100
    let width : CGFloat = 100
    (arrayOfSongsURL as NSArray).enumerateObjectsUsingBlock { (url, index, finished) -> Void in

        let v = UIView(frame: CGRectMake(0, CGFloat(index) * height, width, height))
        v.tag = self.startingTag + index

        v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTapGesture(_:))))
        self.view.addSubview(v)
    }
    // Do any additional setup after loading the view, typically from a nib.
}

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


func handleTapGesture(gesture : UITapGestureRecognizer)
{
    let v = gesture.view!
    let tag = v.tag
    let songURL = arrayOfSongsURL[tag - startingTag]

    //Do what you want to do with songURL
}

}

关于快速uitapgesturerecognizer传递参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38183643/

10-09 16:52