我正在尝试使用 UITapGestureRecognizer
的操作调用带参数的函数,但我想不出任何替代方法。
这是假设使用 indexPath
参数调用 doubleTap 函数的手势。
var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)")
这是假设要调用的函数。
func doubleTap(indexPath: NSIndexPath) {
NSLog("double tap")
NSLog("%@", indexPath.row)
}
如何使用 indexPath
参数调用 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")
}
}
最佳答案
鉴于您有 NSIndexPath
,我想您希望从 UITableView
上的点击触摸点检索相应的 indexPath
。
UIGestureRecognizer
有一个(或没有)参数。当提供一个时,它会传递自身 - 但是,indexPath
不会如前所述传递给函数。
假设我们有以下内容:
let aTap = UITapGestureRecognizer(target: self, action: "tapped:")
点击 View 时对应的函数:
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!"
}
更新:
这用于显示如何向 View 添加手势识别器,通过它我们检索 cell
(item
) 的 indexPath
被点击了两次。
回调函数被触发,在回调函数中我们可以检查点击的cell
(item
)是否是我们感兴趣的。
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)
}
关于ios - 在 Swift 中使用 UITapGestureRecognizer 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085396/