首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Swift中使用UITapGestureRecognizer的动作参数

在Swift中使用UITapGestureRecognizer的动作参数
EN

Stack Overflow用户
提问于 2015-08-19 10:04:04
回答 5查看 24.5K关注 0票数 18

我试图使用UITapGestureRecognizer的操作调用一个带参数的函数,但我找不到任何替代方法。

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

代码语言:javascript
复制
var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap(indexPath)")

这就是应该调用的函数。

代码语言:javascript
复制
func doubleTap(indexPath: NSIndexPath) {
    NSLog("double tap")
    NSLog("%@", indexPath.row)
}

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

感谢您的所有建议。

编辑名称--这是我的全部代码,它基本上是设置对象的‘’,这样我的第二个viewController就可以获取它并使用它

代码语言:javascript
复制
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")
    }
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-08-19 14:45:05

假设您有NSIndexPath,我想您会希望从UITableView上的点击触摸点检索相应的indexPath

UIGestureRecognizer有一个(或没有)参数。当提供了一个函数时,它会自动传递--但是,indexPath不会像前面提到的那样传递给函数。

假设我们有以下内容:

代码语言:javascript
复制
let aTap = UITapGestureRecognizer(target: self, action: "tapped:")

以及点击视图时对应的函数:

代码语言:javascript
复制
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)是否是我们感兴趣的那个。

代码语言:javascript
复制
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(....)
        }
    }
}

的另一个更新:

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

因此:

代码语言:javascript
复制
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)
}
票数 44
EN

Stack Overflow用户

发布于 2016-01-20 19:45:03

最好的方法来实现你的愿望是获得你的点击手势的superview,这将给你正确的indexPath。试试这个:

代码语言:javascript
复制
   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。

票数 3
EN

Stack Overflow用户

发布于 2015-08-19 10:14:29

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

代码语言:javascript
复制
var gestureDoubleTap = UITapGestureRecognizer(target: self, action: "doubleTap:")

:告诉计算机你正在使用一个带参数的函数,这个函数是在别的地方创建的。

希望这能有所帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32085396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档