我需要传递的第二个参数与UILongPressGestureRecognizer's
selector
如何通过一个额外的参数
let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
我需要发送那是很久也按下电池。有没有办法做到这一点
在此先感谢
我需要传递的第二个参数与UILongPressGestureRecognizer's
selector
如何通过一个额外的参数
let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
我需要发送那是很久也按下电池。有没有办法做到这一点
在此先感谢
如果函数有两个参数,如下面。
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:)
我假设您的意思是您要将视图发送到动作函数,并且手势已添加到该视图。
在这种情况下,您可以从传递给动作函数的手势中获取view
。
它看起来像你的动作函数目前没有采用任何基于你正在使用的选择器的参数,所以你也需要纠正它。
您需要添加
在(_ :)
#selector(didLongPressCell(_:))
你方法看起来像
let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell(_:)))
func didLongPressCell(sender: UIView!) -> Void {
//Your sender here is a cell
}
首先改变你的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.
}
}
您可以使用手势.view
属性来获取长按视图中添加这种didLongPressCell
方法。
尝试做如下:
func didLongPressCell(gesture:UILongPressGestureRecognizer) {
let cell: UITableViewCell = gesture.view as! UITableViewCell
print(cell.textLabel?.text)
//use this cell
}
UILongPressGestureRecognizer有一个属性“视图”,它会告诉你,手势连接到视图。 – childrenOurFuture