swift2 Swift中UITapGestureRecognizer动作参数的使用

yhqotfr8  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(258)

我试图使用UITapGestureRecognizer的操作调用一个带参数的函数,但我找不到任何替代方法。
这是一个手势,它应该调用带有indexPath参数的doubleTap函数。

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

这就是我们要调用的函数。

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

如何使用indexPath参数调用doubleTap函数?
感谢您发送编修。

EDIT-这是我的全部代码,它基本上是设置对象“name”,以便我的第二个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")
    }
}
cfh9epnr

cfh9epnr1#

假设您有NSIndexPath,我想您会希望从UITableView上的tap触摸点检索相应的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!"
 }

更新日期:

这用来展示如何向视图添加手势识别器,通过该识别器,我们可以检索被点击两次的cellitem)的indexPath
回调函数被触发,我们可以在其中检查被点击的cellitem)是否是我们感兴趣的那个。

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

rseugnpd2#

最好的方法来实现你的愿望是得到你的点击手势的超级视图,这将给予你正确的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)
    }

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

6za6bjd0

6za6bjd03#

如apple developer文档中所述,action参数应接收

**action::-**一个选择器,用于标识由目的实现得方法,以处理接收器识别得笔势.操作选择器必须符合类概述中描述得签名. NULL不是有效值.

UIGestureRecognizer. h中描述的有效操作方法签名为:
// -(空)句柄手势;// -(void)handleGesture:(UI手势识别器 *)手势识别器;
因此,基本上,除了gestureRecognizer之外,您不能将任何其他内容作为参数发送。

utugiqy6

utugiqy64#

您所需要做的就是调用它,而不需要在字符串文字中使用任何类型的参数。

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

:告诉计算机您正在使用一个带参数的函数,并且该函数是在其他地方创建的。
希望这对你有帮助:)

db2dz4w8

db2dz4w85#

对于user2277872的响应,我也发现它很有用,并且只是对其他可能不熟悉的人有用--IBAction函数 * 也 * 可以通过从xib拖动到类中的 * 预先编写的 * 函数来连接'-如果不需要参数(即没有前面提到的:字符),这很有用。

不带参数的示例

@IBAction private func somethingTapped() {
        ...
}

相关问题