Ios 将collectionview从函数传递到选择器(UILongPressGestureRecognitor)

Ios 将collectionview从函数传递到选择器(UILongPressGestureRecognitor),ios,swift,uicollectionview,uitapgesturerecognizer,uilongpressgesturerecogni,Ios,Swift,Uicollectionview,Uitapgesturerecognizer,Uilongpressgesturerecogni,因此,我使用了一个功能,用户可以长按UICollectionView(注意:我的屏幕上有多个收藏视图)。这会触发一个操作,但当我尝试将收藏视图从我的longPressFolder功能传递到handleLongPress功能时,它不起作用 override func viewDidLoad() { super.viewDidLoad() // add long press to collection View longPres

因此,我使用了一个功能,用户可以长按
UICollectionView
(注意:我的屏幕上有多个收藏视图)。这会触发一个操作,但当我尝试将收藏视图从我的
longPressFolder
功能传递到
handleLongPress
功能时,它不起作用

    override func viewDidLoad() {
        super.viewDidLoad()       

        // add long press to collection View
        longPressFolder(newestFoldersCollectionView)
        longPressFolder(topFoldersCollectionView)
}

   func longPressFolder(collectionview: UICollectionView) {
        // Long press
        let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(FolderOverviewController.handleLongPress(_:)))
        lpgr.minimumPressDuration = 0.4
        lpgr.delegate = self
        lpgr.delaysTouchesBegan = true
        collectionview.addGestureRecognizer(lpgr)
    }
这是代码不起作用的部分。它说我的集合视图未解析,但我似乎找不到将集合视图从一个函数传递到另一个函数的方法

// long press
func handleLongPress(gestureRecognizer : UILongPressGestureRecognizer){
    if (gestureRecognizer.state != UIGestureRecognizerState.Ended){
        return
    }

    let p = gestureRecognizer.locationInView(collectionview)

    if let indexPath: NSIndexPath = (collectionview.indexPathForItemAtPoint(p))!{
        //do whatever you need to do
        ...               
        }
        collectionview.reloadData()
    }        
}
替换

if let indexPath: NSIndexPath = (collectionview.indexPathForItemAtPoint(p))!{

替换

if let indexPath: NSIndexPath = (collectionview.indexPathForItemAtPoint(p))!{


如果在识别器上使用
.view
,则可以获取对手势识别器视图的引用。因此,请尝试:

let collectionview = gestureRecognizer.view as! UICollectionView

如果在识别器上使用
.view
,则可以获取对手势识别器视图的引用。因此,请尝试:

let collectionview = gestureRecognizer.view as! UICollectionView


您应该将视图传递给
locationInView()
。你的
handleLongPress
函数中的
collectionview
变量是从哪里来的?@shim现在不是,问题是我似乎无法将我的collectionview传递给handleLongPress函数。如果你在识别器上使用
.view
,你可以获得手势识别器视图的引用。因此,请尝试将collectionview=gestureRecognitor.view设置为!UICollectionViewHmmm,如果我尝试此操作,并按下第二个collectionview中的第二个单元格,它将选择第一个collectionview中的第二个单元格。。但至少它是进步的:)我会更新代码,这样你就可以检查我是否做了任何不正确的事情。@shim请回答你的解决方案,我会把它标记为正确的。选择collectionview是愚蠢的^^^您应该将视图传递给
locationInView()
。你的
handleLongPress
函数中的
collectionview
变量是从哪里来的?@shim现在不是,问题是我似乎无法将我的collectionview传递给handleLongPress函数。如果你在识别器上使用
.view
,你可以获得手势识别器视图的引用。因此,请尝试将collectionview=gestureRecognitor.view设置为!UICollectionViewHmmm,如果我尝试此操作,并按下第二个collectionview中的第二个单元格,它将选择第一个collectionview中的第二个单元格。。但至少它是进步的:)我会更新代码,这样你就可以检查我是否做了任何不正确的事情。@shim请回答你的解决方案,我会把它标记为正确的。在选择collectionview时很愚蠢^^没有解决我的问题。将我的代码更新为新版本,该版本有效,但仅适用于第一个collectionview。即使我按下第二个按钮。您是否在
longPressFolder
handleLongPress
之外声明了
collectionview
变量?并且您的集合视图没有相互重叠?请尝试将手势识别器仅添加到第二个集合视图中,然后查看,它是否有效?无法解决我的问题。将我的代码更新为新版本,该版本有效,但仅适用于第一个collectionview。即使我按下第二个按钮。您是否在
LongPress文件夹
handleLongPress
之外声明了
collectionview
变量?并且您的collectionview没有相互重叠?请尝试仅将手势识别器添加到第二个集合视图,然后查看,它是否有效