title: Collection View (Creating a Draggable UICollectionViewCell) Fatal Error
body: <p>I am trying to get drag and drop functionality for cells in a collection view. Similar to the one below, except that this one is for a simple UIView:
  <a href="https://www.cocoacontrols.com/controls/lgdragdrop" rel="nofollow">https://www.cocoacontrols.com/controls/lgdragdrop</a></p>
  
  <p>To extend this to a Collection View, I am relying on this neat little hack
  <a href="https://adoptioncurve.net/archives/2014/07/creating-a-draggable-uicollectionviewcell/" rel="nofollow">https://adoptioncurve.net/archives/2014/07/creating-a-draggable-uicollectionviewcell/</a></p>
  
  <p>Here is the hack
  "Create a copy of the contents of the cell as an image, then drag this around the screen underneath your finger."</p>
  
  <p>I have converted the code into Swift:</p>
  
  <pre><code>func handlePan(panRecognizer: UIPanGestureRecognizer){
      var locationPoint: CGPoint! = panRecognizer.locationInView(self.collectionView)
  
      //var locationPoint: CGPoint = panRecognizer.locationInView(self.collectionView)
  
      if panRecognizer.state == .Began {
          print("Pan Gesture BEGAN")
  
          var indexPathOfMovingCell: NSIndexPath = self.collectionView.indexPathForItemAtPoint(locationPoint)!
          var cell: UICollectionViewCell = self.collectionView.cellForItemAtIndexPath(indexPathOfMovingCell)!
          UIGraphicsBeginImageContext(cell.bounds.size)
          cell.layer.renderInContext(UIGraphicsGetCurrentContext()!)
          var cellImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
  
          self.movingCell = UIImageView(image: cellImage)
          //self.movingCell.userInteractionEnabled = true
          self.movingCell.center = locationPoint
          self.movingCell.alpha = 0.75
          self.collectionView.addSubview(self.movingCell)
      }
      if panRecognizer.state == .Changed {
          print("Pan Gesture CHANGED")
  
          self.movingCell.center = locationPoint
      }
      if panRecognizer.state == .Ended {
          print("Pan Gesture ENDED")
  
          self.movingCell.removeFromSuperview()
      }
  
  }`
  </code></pre>
  
  <p>However, I am getting an error </p>
  
  <pre><code>Pan Gesture BEGAN
  fatal error: unexpectedly found nil while unwrapping an Optional value
  (lldb)
  </code></pre>
  
  <p>in the following line</p>
  
  <pre><code>self.movingCell.center = locationPoint
  </code></pre>
  
  <p>There are other posts related to "unwrapping an Optional value" but didn't help me in my specific case</p>
  
  <p>Please help...</p>
  
  <p>Also, I am not sure if the cell can be dragged outside the collection view. If not, what code needs to be added for that.</p>