Skip to content

Instantly share code, notes, and snippets.

@timd
Created July 16, 2014 15:27
Show Gist options
  • Save timd/31e2e5bd75e99405bc35 to your computer and use it in GitHub Desktop.
Save timd/31e2e5bd75e99405bc35 to your computer and use it in GitHub Desktop.
A draggable UICollectionViewCell hack
(void)handlePan:(UIPanGestureRecognizer *)panRecognizer {
CGPoint locationPoint = [panRecognizer locationInView:self.collectionView];
if (panRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath indexPathOfMovingCell = [self.collectionView indexPathForItemAtPoint:locationPoint];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPathOfMovingCell];
UIGraphicsBeginImageContext(cell.bounds.size);
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.movingCell = [[UIImageView alloc] initWithImage:cellImage];
[self.movingCell setCenter:locationPoint];
[self.movingCell setAlpha:0.75f];
[self.collectionView addSubview:self.movingCell];
}
if (panRecognizer.state == UIGestureRecognizerStateEnded) {
[self.movingCell removeFromSuperview];
}
if (panRecognizer.state == UIGestureRecognizerStateChanged) {
[self.movingCell setCenter:locationPoint];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment