简体   繁体   English

带有手势识别器的UICollectionView

[英]UICollectionView with gesture recogniser

I have a UICollectionView which has a long press gesture attached to it. 我有一个UICollectionView带有长按手势。 It works fine when i'm pressing on a cell but if the touched area isn't a cell the app crashes with EXC_BREAKPOINT 当我按一个单元格时它可以正常工作,但是如果触摸的区域不是一个单元格,则应用程序会崩溃并显示EXC_BREAKPOINT

It crashes on the 它崩溃了

if let indexPath : NSIndexPath = collectView.indexPathForItemAtPoint(point)! { 

line. 线。 I believe that i need to check that the point is actually a cell but i'm not sure what to check for 我相信我需要检查该点实际上是一个单元格,但是我不确定要检查什么

the code is as follows 代码如下

@IBAction func longPressCell(sender: UILongPressGestureRecognizer) {
    if (sender.state == UIGestureRecognizerState.Began) {
        if let point : CGPoint = sender.locationInView(self.collectionView) {
            if let collectView = self.collectionView {
                if let indexPath : NSIndexPath = collectView.indexPathForItemAtPoint(point)! {
                    let adopt : UserPet = self.fetchedResultsController.objectAtIndexPath(indexPath) as! UserPet
                    NSLog("Adopt: \(adopt)")
                }
            }
        }

    }
}

collectView.indexPathForItemAtPoint(point)!= nil {已解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM