Created
July 16, 2014 15:27
-
-
Save timd/31e2e5bd75e99405bc35 to your computer and use it in GitHub Desktop.
A draggable UICollectionViewCell hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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