[英]Pass multiple parameters to gesture recogniser
我正在研究 UITableView。 每個單元格都有一系列 UIImageViews,它們的 url 被保存到一個數組中。 當我點擊某個圖像時,我的手勢識別器的選擇器應該同時獲得圖像的標簽和單元格的indexPath.row
。
我目前正在從sender
參數中獲取圖像標簽,這是我嘗試獲取索引路徑的方式:
let point = sender.locationInView(self.tableView)
let indexPathRow = self.tableView.indexPathForRowAtPoint(point)?.row
但是,這似乎沒有給我正確的行。 有沒有一種簡單的方法可以將這兩個參數傳遞給我的手勢選擇器? 謝謝。
您可以子類化您正在使用的手勢識別器並添加您想要的額外變量。 下面舉例。
class CustomGestureRecognizer : UITapGestureRecognizer {
var url : NSURL?
// any more custom variables here
init(target: AnyObject?, action: Selector, url : NSURL) {
super.init(target: target, action: action)
self.url = url
}
}
然后當你想要獲取 url 時。
func didTap(sender : CustomGestureRecognizer) {
print(sender.url)
}
我認為最簡單的方法是將 UIImageView 子類化以保存您的 NSIndexPath:
class MyImageView: UIImageView {
var indexpath:NSIndexPath!
}
然后獲取該 indexPath:
func tap(tap: UITapgestureRecognizer) {
let imgView = tap.view as! MyImageView
let indexPath = imgView.indexPath
let tag = imgView.tag
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.