我试图使用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")
}
}
发布于 2015-08-19 14:45:05
假设您有NSIndexPath
,我想您会希望从UITableView
上的点击触摸点检索相应的indexPath
。
UIGestureRecognizer
有一个(或没有)参数。当提供了一个函数时,它会自动传递--但是,indexPath
不会像前面提到的那样传递给函数。
假设我们有以下内容:
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!"
}
更新:
这用于展示如何向视图添加一个手势识别器,通过它我们可以检索被点击两次的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)
}
发布于 2016-01-20 19:45:03
最好的方法来实现你的愿望是获得你的点击手势的superview,这将给你正确的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)
}
您可以根据您的层次结构级别增加或减少superview。
发布于 2015-08-19 10:14:29
你所需要做的就是调用它,而不需要在字符串中使用任何类型的参数。
var gestureDoubleTap = UITapGestureRecognizer(target: self, action: "doubleTap:")
:
告诉计算机你正在使用一个带参数的函数,这个函数是在别的地方创建的。
希望这能有所帮助:)
https://stackoverflow.com/questions/32085396
复制相似问题