I am currently able to make an image scale by using the pinch gesture recognizer provided by Apple, but how can I limit the maximum scale for the image?
I have been using the SimpleGestureRecognizers sample from Apple, which sets the scale to 1, and makes it very easy to scale image, but when I remove it, it jumps suddenly, which is not nice. But then it is hard to track the scale since its alway set to 1.
Thank you in advance.
UIKit will call your selector method many times for a single user gesture (pinch.) Each call will represent a small increment (or decrease) in the scale. If you handle each call to the method and scale your image correspondingly for each small increment, you will get a smooth animation.
You use the recognizer's scale property to modify your image. Then, you reset the property to 1.0 so that the next time the selector method is called, the scale property represents only the new small increment since the last one. You again re-scale your image by the new small increment and reset to 1.0. If you do not reset the recognizer's scale and use it to re-scale your image, you will be scaling your image by the cumulative change, resulting in jumpy behavior (and incorrect scaling.)
So, do make sure to be resetting the gesture recognizer's scale to 1. To keep track of the total change in scale, you can create an instance variable, possibly also called 'scale', for your UIImageView or your ViewController. (In fact, if you used that new scale variable in drawRect to draw your image, you will only have to add a call to setNeedsDisplay in the setter. And you won't have to write any code to redraw the image when the scale changes.)
In any case, the following code (incorporating Ravin's limit check, but on the new scale variable) should work for your selector method:
I hope this is helpful.
In selector method of
gestureRecognizer
check forscale
property ofsender
(UIPinchGestureRecognizer
) if it's less than you wish to scale, apply scaling on image else dont. For example following method isselector
forpinchGesture
then .Thanks,
If you're only doing this in one place and you don't want to sub-class UIView or UIImageView you can create some local variables to achieve the same result.
Swift
Apple Documentation Handling Pinch Gestures