繁体   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