0

我需要傳遞的第二個參數與UILongPressGestureRecognizer'sselector如何通過一個額外的參數

let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell)) 

我需要發送那是很久也按下電池。有沒有辦法做到這一點

在此先感謝

+0

UILongPressGestureRecognizer有一個屬性「視圖」,它會告訴你,手勢連接到視圖。 – childrenOurFuture

回答

1

如果函數有兩個參數,如下面。

func clicked(sender:AnyObject,value:AnyObject) 
{ 
} 

然後

action = "clicked::" 

例如:

func switchCard(card: Int, withCard card1: Int) 
{ 
    print(card) 
} 

let singleTap1 = UITapGestureRecognizer(target: self, action: "switchCard:withCard:") 

就在上雨燕2.2注。您現在可以鍵入選擇器

#selector(popoverSelectedCode(_:desc:) 
0

我假設您的意思是您要將視圖發送到動作函數,並且手勢已添加到該視圖。

在這種情況下,您可以從傳遞給動作函數的手勢中獲取view

它看起來像你的動作函數目前沒有采用任何基於你正在使用的選擇器的參數,所以你也需要糾正它。

0

您需要添加

(_ :)

#selector(didLongPressCell(_:))

你方法看起來像

let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell(_:))) 

func didLongPressCell(sender: UIView!) -> Void { 
     //Your sender here is a cell 
} 
1

首先改變你的selector語法UILongPressGestureRecognizer這樣

#selector(self.didLongPressCell(_:)) 

現在,在您viewController

func didLongPressCell(gesture: UILongPressGestureRecognizer) { 
    if (gesture.state == .Ended) { 
     let point = gesture.locationInView(self.tableView) 
     let indexPath = self.tableView.indexPathForRowAtPoint(point) 
     let customCell = self.tableView.cellForRowAtIndexPath(indexPath) as! CustomCell 
     //This is the cell that you want. 
    } 
} 
1

您可以使用手勢.view屬性來獲取長按視圖中添加這種didLongPressCell方法。

嘗試做如下:

func didLongPressCell(gesture:UILongPressGestureRecognizer) { 
    let cell: UITableViewCell = gesture.view as! UITableViewCell 
    print(cell.textLabel?.text) 
    //use this cell 
} 
相關問題