在Swift中使用UITapGestureRecognizer中的参数 [英] Using parameters in action of UITapGestureRecognizer in Swift

查看:1788
本文介绍了在Swift中使用UITapGestureRecognizer中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 UITapGestureRecognizer 的操作调用带参数的函数,我无法找出任何替代方法。

I am trying to call a function with parameters using the action of UITapGestureRecognizer and I can't figure out any alternative.

这是假设用 indexPath 参数调用doubleTap函数的手势。

This here is the gesture that is suppose to call the doubleTap function with the indexPath parameter.

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

这是假设要调用的函数。

This here is the function that is suppose to be called.

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

如何使用 indexPath 参数调用 doubleTap 函数?

How can I call the doubleTap function with the indexPath parameter?

感谢您提出的所有建议。

Thank you for all suggestions.

编辑 - 这是我的整个代码,它基本上是设置对象'名称'所以我的第二个viewController可以获取并使用它

EDIT - this is my whole code, it is basically setting up the object 'name' so my second viewController can get it and use it

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 ,我想你想从点击触摸点检索相应的 indexPath 在您的 UITableView

Given that you have NSIndexPath, I suppose you would like to retrieve the corresponding indexPath from the tap touch point on your UITableView.

UIGestureRecognizer 有一个(或没有)参数。如果提供了一个,它会自行传递 - indexPath 然而, not 传递给上述函数。

UIGestureRecognizer has one (or no) parameter. When there is one provided, it passes itself - the indexPath is, however, not passed to the function as mentioned.

假设我们有以下内容:

let aTap = UITapGestureRecognizer(target: self, action: "tapped:")

以及点击视图时的相应功能:

and the corresponding function when the view is tapped on:

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!"
 }

更新:

Update:

这用于显示如何向视图添加手势识别器,通过它我们检索 indexPath 被点击两次的单元格 item )。

This serves to show how to add a gesture recognizer to the view, through which we retrieve the indexPath of the cell (item) that was tapped on twice.

触发回调函数,我们可以在其中检查是否点击了单元格 item )是我们感兴趣的。

The callback function is triggered, within which we could check whether or not the cell (item) that was tapped on is the one we are interested in.

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(....)
        }
    }
}

另一个更新:

Another Update:

由于您似乎正在添加需要双击所有单元格的手势识别器,这告诉我您对双击的任何单元格感兴趣上;因此,我们不需要任何条件来检查单元格是否是我们感兴趣的单元格,因为它们都是。

Since it appears that you are adding gesture recognizer that requires double taps to all cells, which tells me that you are interested in any cell that was double-tapped on; and so, we would't need any condition to check whether or not the cells are the ones we are interested in, because they all are.

所以:

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

这篇关于在Swift中使用UITapGestureRecognizer中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆