簡體   English   中英

將collectionview從函數傳遞到選擇器(UILongPressGestureRecognizer)

[英]Pass collectionview from function to Selector (UILongPressGestureRecognizer)

因此,我正在使用用戶可以在UICollectionView上長按的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 = ((gestureRecognizer.view as! UICollectionView).indexPathForItemAtPoint(p))!{

如果在識別器上使用.view ,則可以獲取對手勢識別器視圖的引用。 因此,請嘗試:

let collectionview = gestureRecognizer.view as! UICollectionView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM