简体   繁体   中英

UICollectionView with gesture recogniser

I have a UICollectionView which has a long press gesture attached to it. 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

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 {已解决

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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