Ios 在Swift中使用UITapGestureRecognitor的参数

Ios 在Swift中使用UITapGestureRecognitor的参数,ios,swift,uitableview,swift2,uigesturerecognizer,Ios,Swift,Uitableview,Swift2,Uigesturerecognizer,我试图使用uitappesturerecognizer的操作调用带有参数的函数,但我找不出任何替代方法 这是假定使用indepath参数调用doubleTap函数的手势 var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)") 这是假定要调用的函数 func doubleTap(indexPath: NSIndexPath

我试图使用
uitappesturerecognizer
的操作调用带有参数的函数,但我找不出任何替代方法

这是假定使用
indepath
参数调用doubleTap函数的手势

var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)")
这是假定要调用的函数

func doubleTap(indexPath: NSIndexPath) {
    NSLog("double tap")
    NSLog("%@", indexPath.row)
}
如何使用
indepath
参数调用
doubleTap
函数

谢谢你的建议

编辑-这是我的全部代码,它基本上是设置对象“名称”,以便我的第二个viewController可以获取并使用它

import UIKit
class viewController1: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionView: UICollectionView!
    var imageArray:[String] = []
    var name : AnyObject? {
        get {
        return NSUserDefaults.standardUserDefaults().objectForKey("name")
        }
        set {
            NSUserDefaults.standardUserDefaults().setObject(newValue!, forKey: "name")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as myViewCell

        //adding single and double tap gestures for each cell
        /////////////////////////////
        //ISSUE IS SENDING indexPath TO doubleTap FUNC
        var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap:")
        gestureDoubleTap.numberOfTapsRequired = 2
        cell.addGestureRecognizer(gestureDoubleTap)

        var gestureSingleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTap")
        gestureSingleTap.numberOfTapsRequired = 1
        cell.addGestureRecognizer(gestureSingleTap)

        cell.imgView.image=UIImage(named: imageArray[indexPath.row])        
        return cell
    }

    //func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    //
    //    name = imageArray[indexPath.row]
    //}

    override func viewDidLoad(){
        super.viewDidLoad()
        imageArray=["1.png","2.png","2.png","1.png","1.png","2.png","1.png","2.png","1.png","2.png","1.png","2.png","1.png","2.png","1.png","2.png"]

    }

    func doubleTap(sender: UITapGestureRecognizer) {
        var tapLocation = sender.locationInView(self.collectionView)

        var indexPath:NSIndexPath = self.collectionView.indexPathForItemAtPoint(tapLocation)!

        //var cell = self.collectionView.cellForItemAtIndexPath(indexPath)

        NSLog("double tap")
        NSLog("%@", indexPath)

        //NSLog("%@", cell!)
        //THIS IS THE GOAL----- set 'name' with the appropriate img      corresponding the cell
        //name = imageArray[indexPath]
        //self.performSegueWithIdentifier("segue", sender: nil)
    }

    func singleTap() {
        NSLog("single tap")
    }
}

您所需要做的就是调用它,而不在字符串文本中使用任何类型的参数

var gestureDoubleTap = UITapGestureRecognizer(target: self, action: "doubleTap:")
告诉计算机您正在使用带有参数的函数,该函数是在其他地方创建的


希望这有帮助:)

鉴于您有
nsindepath
,我想您希望从
UITableView
上的点击点检索相应的
indepath

UIgestureRecognitor
有一个(或没有)参数。当提供了一个时,它会传递自己,
indepath
不会像前面提到的那样传递给函数

假设我们有以下几点:

let aTap = UITapGestureRecognizer(target: self, action: "tapped:")
以及打开视图时的相应功能:

func tapped(sender: UITapGestureRecognizer)
{
    //using sender, we can get the point in respect to the table view
    let tapLocation = sender.locationInView(self.tableView)

    //using the tapLocation, we retrieve the corresponding indexPath
    let indexPath = self.tableView.indexPathForRowAtPoint(tapLocation)

    //finally, we print out the value
    print(indexPath)

    //we could even get the cell from the index, too
    let cell = self.tableView.cellForRowAtIndexPath(indexPath!)

    cell.textLabel?.text = "Hello, Cell!"
 }
更新:

这说明了如何将手势识别器添加到视图中,通过该视图,我们可以检索两次点击的
单元格(
)的
索引XPath

将触发回调函数,在该函数中,我们可以检查被点击的
单元格
)是否是我们感兴趣的单元格

override func viewDidLoad()
{
    super.viewDidLoad()

    let doubleTaps = UITapGestureRecognizer(target: self, action: "doubleTapTriggered:")
    doubleTaps.numberOfTapsRequired = 2
    self.view.addGestureRecognizer(doubleTaps)
}

func doubleTapTriggered(sender : UITapGestureRecognizer)
{
    var tapLocation = sender.locationInView(self.collectionView)
    var indexPath : NSIndexPath = self.collectionView.indexPathForItemAtPoint(tapLocation)!

    if let cell = self.collectionView.cellForItemAtIndexPath(indexPath)
    {
        if(cell.tag == 100)
        {
            print("Hello, I am cell with tag 100")
        }
        else if(cell.tag == 99)
        {
            print("Hello, I am cell with tag 99")
            //We could do something, then, with the cell that we are interested in.
            //I.e., cell.contentView.addSubview(....)
        }
    }
}
另一个更新:

因为看起来你们正在添加需要双击所有单元格的手势识别器,这告诉我你们对任何双击过的单元格都感兴趣;因此,我们不需要任何条件来检查这些细胞是否是我们感兴趣的细胞,因为它们都是

因此:

func doubleTapTriggered(sender : UITapGestureRecognizer)
{
    var tapLocation = sender.locationInView(self.collectionView)
    var indexPath : NSIndexPath = self.collectionView.indexPathForItemAtPoint(tapLocation)!

    name = imageArray[indexPath]
    self.performSegueWithIdentifier("segue", sender: nil)
}

正如apple developer文档中所述,action参数应该

操作:- 一种选择器,用于识别目标执行的处理接收者识别的手势的方法。操作选择器必须符合类概述中描述的签名。NULL不是有效值

UIgestureRecognitor.h中描述的有效操作方法签名为:

//-(无效)手感; //-(无效)手势识别器:(UIgestureRecognitor*)手势识别器


因此,基本上,除了手势识别器,您将无法将任何其他内容作为参数发送。

实现所需的最佳方法是获得点击手势的超视图,这将为您提供正确的索引。试试这个:

   func doubleTap(sender: UITapGestureRecognizer) {
        let point = sender.view
        let mainCell = point?.superview
        let main = mainCell?.superview
        let cell: myViewCell = main as! myViewCell
        let indexPath = collectionView.indexPathForCell(cell)
    }

您可以根据您的层次结构级别增加或减少superview。

进一步了解
user2277872
的响应,我发现它也很有用,对于可能不熟悉的任何其他人来说,
iAction
函数也可以通过从xib拖动到类中预先编写的函数来连接-如果不需要参数(即没有前面提到的
字符),这非常有用

无参数示例

@IBAction private func somethingTapped() {
        ...
}

好吧,这对我来说有点道理,但是doubleTap func实际上是如何接收indexPath变量的呢?它将通过您的操作调用。被调用的函数处理所有参数信息好吧,这是有意义的,除了我使用的是UICollectionView,我猜这是相同的过程,我可以得到UICollectionCell的tapLocation和indexPath?我似乎无法从indexPath得到一个整数。当我打印indexPath时,我在控制台中得到“0xc000000000000016>{length=2,path=0-0}”。如何从这个索引中获取0?indexPath.row不起作用,它说它不是INDEXPATHI的成员如果我将手势识别器添加到视图中,任何轻触或双击都会触发手势,我需要它,以便轻触或双击时每个单元格都有自己的功能条件会放在哪里?在doubleTap(发送方:UITapgestureEncognizer)功能中?你能给我一个更具体的例子吗?我在if条件下做了NSLog(“@”,cell.tag),它总是返回null,实际上我没有得到一个标识,这绝不是最好的方法。特别是考虑到这个问题和现有的答案。