0

そのため、ユーザーが長押しできる機能を使用しています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()
    }        
}
4

2 に答える 2

1

交換

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

if let indexPath: NSIndexPath = ((gestureRecognizer.view as! UICollectionView).indexPathForItemAtPoint(p))!{
于 2016-07-13T14:52:06.457 に答える