ios – 使用Swift中UITapGestureRecognizer的操作参数

前端之家收集整理的这篇文章主要介绍了ios – 使用Swift中UITapGestureRecognizer的操作参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用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","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:")

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

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

更新:

这用于显示如何在视图中添加手势识别器,通过该视图,我们可以检索分隔了两次的单元格(item)的indexPath.

触发回调函数,我们可以在其中检查被敲击的单元格(项目)是否是我们感兴趣的.

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)
}
原文链接:https://www.f2er.com/iOS/337557.html

猜你在找的iOS相关文章