-
-
Save majindageta/beacf37968f31aa707fdcfd925ef01d5 to your computer and use it in GitHub Desktop.
Handle pinch gesture limiting size
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)handlePinch:(UIPinchGestureRecognizer *)recognizer { | |
if (UIGestureRecognizerStateBegan == recognizer.state || | |
UIGestureRecognizerStateChanged == recognizer.state) { | |
// Use the x or y scale, they should be the same for typical zooming (non-skewing) | |
float currentScale = [[recognizer.view.layer valueForKeyPath:@"transform.scale.x"] floatValue]; | |
// Variables to adjust the max/min values of zoom | |
float minScale = 0.7; | |
float maxScale = 2.0; | |
float zoomSpeed = .5; | |
float deltaScale = recognizer.scale; | |
// You need to translate the zoom to 0 (origin) so that you | |
// can multiply a speed factor and then translate back to "zoomSpace" around 1 | |
deltaScale = ((deltaScale - 1) * zoomSpeed) + 1; | |
// Limit to min/max size (i.e maxScale = 2, current scale = 2, 2/2 = 1.0) | |
// A deltaScale is ~0.99 for decreasing or ~1.01 for increasing | |
// A deltaScale of 1.0 will maintain the zoom size | |
deltaScale = MIN(deltaScale, maxScale / currentScale); | |
deltaScale = MAX(deltaScale, minScale / currentScale); | |
CGAffineTransform zoomTransform = CGAffineTransformScale(recognizer.view.transform, deltaScale, deltaScale); | |
recognizer.view.transform = zoomTransform; | |
// Reset to 1 for scale delta's | |
// Note: not 0, or we won't see a size: 0 * width = 0 | |
recognizer.scale = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment