Skip to content

Instantly share code, notes, and snippets.

@ngutman
Last active January 3, 2016 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ngutman/8483704 to your computer and use it in GitHub Desktop.
Save ngutman/8483704 to your computer and use it in GitHub Desktop.
- (void)dragged:(UIPanGestureRecognizer *)gestureRecognizer
{
CGFloat xDistance = [gestureRecognizer translationInView:self].x;
CGFloat yDistance = [gestureRecognizer translationInView:self].y;
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:{
self.originalPoint = self.center;
break;
};
case UIGestureRecognizerStateChanged:{
CGFloat rotationStrength = MIN(xDistance / 320, 1);
CGFloat rotationAngel = (CGFloat) (2*M_PI/16 * rotationStrength);
CGFloat scaleStrength = 1 - fabsf(rotationStrength) / 4;
CGFloat scale = MAX(scaleStrength, 0.93);
CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
self.transform = scaleTransform;
self.center = CGPointMake(self.originalPoint.x + xDistance, self.originalPoint.y + yDistance);
[self updateOverlay:xDistance];
break;
};
case UIGestureRecognizerStateEnded: {
[self resetViewPositionAndTransformations];
break;
};
case UIGestureRecognizerStatePossible:break;
case UIGestureRecognizerStateCancelled:break;
case UIGestureRecognizerStateFailed:break;
}
}
- (void)updateOverlay:(CGFloat)distance
{
if (distance > 0) {
self.overlayView.mode = GGOverlayViewModeRight;
} else if (distance <= 0) {
self.overlayView.mode = GGOverlayViewModeLeft;
}
CGFloat overlayStrength = MIN(fabsf(distance) / 100, 0.4);
self.overlayView.alpha = overlayStrength;
}
- (void)resetViewPositionAndTransformations
{
[UIView animateWithDuration:0.2
animations:^{
self.center = self.originalPoint;
self.transform = CGAffineTransformMakeRotation(0);
self.overlayView.alpha = 0;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment