在Swift中使用UITapGestureRecognizer的参数

我想使用UITapGestureRecognizer的行动与参数调用一个函数,我不能找出任何select。

这里是假设用indexPath参数调用indexPath函数的indexPath

 var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)") 

这里这是假设被调用的函数。

 func doubleTap(indexPath: NSIndexPath) { NSLog("double tap") NSLog("%@", indexPath.row) } 

我如何使用indexPath参数调用indexPath函数?

谢谢你的所有build议。

编辑 – 这是我的整个代码,它基本上设置对象的名称,所以我的第二个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") } } 

鉴于你有NSIndexPath ,我想你想从你的UITableView上的点击触摸点检索相应的indexPath

UIGestureRecognizer有一个(或没有)参数。 当有一个提供的时候,它自己传递 – 但是, indexPath 不会像前面提到的那样传递给函数。

假设我们有以下几点:

 let aTap = UITapGestureRecognizer(target: self, action: "tapped:") 

以及点击视图时的相应function:

 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!" } 

更新:

这用来显示如何添加一个手势识别器到视图,通过它我们检索两次点击的cellitem )的indexPath

触发callback函数,我们可以检查被触发的cell是否是我们感兴趣的cell

 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. //Ie, 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) } 

最好的方式来实现你想要的是得到你的水龙头手势的超级视图,这将给你正确的indexPath。 尝试这个:

  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) } 

您可以根据您的层次结构级别增加或减less超级视图。

如苹果开发者文档中所述,action参数应该被接收

action :: –一个select器,用于标识由目标实现的处理接收器识别的手势的方法。 动作select器必须符合课堂概述中描述的签名。 NULL不是一个有效的值。

在UIGestureRecognizer.h中描述的有效操作方法签名是:

// – (void)handleGesture; // – (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

所以基本上,除了gestureRecognizer之外,您将无法发送任何其他参数作为参数。

所有你需要做的就是调用它,而不使用任何types的参数在你的string文字。

 var gestureDoubleTap = UITapGestureRecognizer(target: self, action: "doubleTap:") 

The :告诉计算机你正在使用一个带有参数的函数,而且是在别的地方创build的。

希望这可以帮助 :)