Sto tentando di chiamare una funzione con parametri utilizzando l'azione di UITapGestureRecognizer
e non riesco a trovare alcuna alternativa.Utilizzo dei parametri in azione di UITapGestureRecognizer in Swift
Questo è il gesto che si suppone chiamino la funzione doubleTap con il parametro indexPath
.
var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)")
Questa qui è la funzione che è supponiamo di essere chiamato.
func doubleTap(indexPath: NSIndexPath) {
NSLog("double tap")
NSLog("%@", indexPath.row)
}
Come posso chiamare la funzione doubleTap
con il parametro indexPath
?
Grazie per tutti i suggerimenti.
EDIT - questo è tutto il mio codice, è fondamentalmente allestendo il 'nome' oggetto in modo mio secondo viewController può ottenere e usarlo
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")
}
}
Ok, questo ha senso, tranne che sto usando UICollectionView, sto indovinando la sua stessa procedura e posso ottenere il tapLocation e indexPath di UICollectionCell? – Cherryholme
Non riesco a ottenere un intero da indexPath. quando stampo indexPath ottengo "0xc000000000000016> {length = 2, path = 0 - 0}" nella mia console. come faccio a ottenere lo 0 da questo indexPath? indexPath.row non funziona, si dice che non è un membro di indexPath – Cherryholme
Se aggiungo il riconoscimento di gesture alla vista di qualsiasi tocco o doppio tocco attiverà il gesto, ne ho bisogno in modo che ogni cella abbia la propria funzione quando viene toccato o toccato due volte – Cherryholme