web-dev-qa-db-ja.com

Swift=でUITapGestureRecognizerのアクションでパラメーターを使用する

UITapGestureRecognizerのアクションを使用してパラメーターを指定して関数を呼び出そうとしていますが、他の方法はわかりません。

これは、indexPathパラメーターを指定してdoubleTap関数を呼び出すことを想定したジェスチャーです。

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

これは、呼び出されると想定される関数です。

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

doubleTapパラメータでindexPath関数を呼び出すにはどうすればよいですか?

すべての提案をありがとう。

[〜#〜] edit [〜#〜]-これは私の全体のコードであり、基本的にオブジェクトを設定しています2番目の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")
    }
}
17
Cherryholme

NSIndexPathがある場合、indexPathのタップタッチポイントから対応するUITableViewを取得するとします。

UIGestureRecognizerにはパラメーターが1つ(またはなし)あります。提供されている場合は、それ自体を渡します。ただし、indexPathは、前述のようにnot関数に渡されます。

次のものがあるとしましょう:

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

更新:

これは、2回タップされたindexPathcell)のitemを取得するビューにジェスチャレコグナイザーを追加する方法を示すのに役立ちます。

コールバック関数がトリガーされ、その中で、タップされた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)
}
42
Unheilig

希望することを実現する最良の方法は、タップジェスチャのスーパービューを取得することです。これにより、正しい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)
    }

階層レベルに応じて、スーパービューを増減できます。

3
Uriel

Apple開発者ドキュメントで説明されているように、actionパラメーターは

action ::-レシーバーによって認識されたジェスチャーを処理するためにターゲットによって実装されるメソッドを識別するセレクター。アクションセレクタは、クラスの概要で説明されている署名に準拠する必要があります。 NULLは有効な値ではありません。

UIGestureRecognizer.hで説明されている有効なアクションメソッドシグネチャは次のとおりです。

//void)handleGesture; //-(void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

したがって、基本的には、---(not gestureRecognizer以外のパラメーターとして何かを送信できます。

0
Vikas Dadheech

あなたがする必要があるのは、文字列リテラルでパラメータの種類を使用せずにそれを呼び出すことです。

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

:は、パラメーターを持つ関数を使用していること、およびそれが別の場所で作成されていることをコンピューターに伝えます。

お役に立てれば :)

0
user2277872