我正在尝试使用以下操作来调用带有参数的函数UITapGestureRecognizer
我想不出任何替代方案。
这是假设调用 doubleTap 函数的手势indexPath
范围。
var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)")
这是应该被调用的函数。
func doubleTap(indexPath: NSIndexPath) {
NSLog("double tap")
NSLog("%@", indexPath.row)
}
我怎样才能打电话给doubleTap
函数与indexPath
范围?
感谢您的所有建议。
EDIT- 这是我的整个代码,它基本上是设置对象“名称”,以便我的第二个 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")
}
}