iOS 10.2 Swift 3.0
Trying to translate this piece of code from Paul Solt blog. Fixed by SO poster, updated code!
http://paulsolt.com/blog/2011/03/limiting-uipinchgesturerecognizer-zoom-levels
Sample Code
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
Almost done, but can not seem to find a reference to the CATransform key used here for Swift 3.0. My code ...
if sender.state == .began {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = sender.scale
}
if sender.state == .began || sender.state == .changed {
// UPDATED
currentScaleX = self.image2P.transform.scaleX
currentScaleY = self.image2P.transform.scaleY
self.image2P.transform = self.image2P.transform.scaledBy(x: 1.1, y: 1.1)
// Constants to adjust the max/min values of zoom
let kMaxScale:CGFloat = 2.0;
let kMinScale:CGFloat = 1.0;
var newScale = 1 - (lastScale - sender.scale) // new scale is in the range (0-1)
newScale = min(newScale, kMaxScale / currentScaleX)
newScale = max(newScale, kMinScale / currentScaleY)
self.image2P.transform = self.image2P.transform.scaledBy(x: newScale, y: newScale)
lastScale = sender.scale // Store the previous scale factor for the next pinch gesture call
}
You should store xScale and yScale separately because generally speaking there is no guarantee they are equal.
extension CGAffineTransform {
var scaleX: CGFloat {
return (a > 0 ? 1 : -1) * sqrt (a*a + c*c)
}
var scaleY: CGFloat {
return (d > 0 ? 1 : -1) * sqrt (b*b + d*d)
}
}
These extension methods will work return correct scale factors even if your view is also rotated and/or translated:
view.transform = CGAffineTransform(scaleX: 1.5, y: 1.2).rotated(by: .pi/3.0).translatedBy(x: 50, y: 30)
print("scaleX = \(view.transform.scaleX), scaleY = \(view.transform.scaleY)")
Output:
scaleX = 1.5, scaleY = 1.2
It may be not obvious, but a, b, c and d properties are elements of the transform matrix. You can find more details in Quartz 2D Programming Guide. Also you can find math details here. Just be aware that b and c element names are swapped in these two sources.
Related
I am fairly new to iOS and I am trying to figure out how to use a Pinch gesture to zoom with inertia and overhead (I do not know if the word overhead is correct in this context, in german it would be called "Überschwingen").
Basically what it shall do: It should have a max and min scale (in my case 1.0 to 4.0) in which you can zoom. When the gesture is finished it should take the given velocity and make a curve out animation, also allowing the view to over- and underflow the given scales and then move back to the min or max like with tension.
I got the Gesture Recognizer running for this and also managed to get it make use of my minimum and maximum scale (using examples from stackoverflow). This is what I got so far:
- (void)handle_pinch:(UIPinchGestureRecognizer *)recognizer
{
if([recognizer state] == UIGestureRecognizerStateBegan) {
previousScale = 1.0;
lastPoint = [recognizer locationInView:[recognizer view]];
}
if ([recognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[recognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 4.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (previousScale - [recognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
scale = newScale;
CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);
[recognizer view].transform = transform;
CGPoint point = [recognizer locationInView:[recognizer view]];
CGAffineTransform transformTranslate = CGAffineTransformTranslate([[recognizer view] transform], point.x-lastPoint.x, point.y-lastPoint.y);
[recognizer view].transform = transformTranslate;
NSLog(#"Transformed");
}
}
But I do now know how I can add the animation here. Thanks for any help!
You should be using a UIScrollView to achieve the pinch to zoom effect as UIScrollView is already integrated along with animation. Just add your UIView inside a UIScrollView.
Here is great tutorial on UIScrollView. He is using a UIImageView but UIView will behave in a similar way.
https://www.raywenderlich.com/122139/uiscrollview-tutorial
I want to move imageView by PanGestureRecognizer like this
(imageView can be scaled)
If imageView's position is (0,0), it can't move.
If imageView's position x is over 60, it can't move more.
If imageView's position y is over 80, it can't move more.
When imageView's scale is restored(1.0), it's position is (0,0).
It is difficult to restrict imageView's moving and position.
What should I do?
Here is my code.
img = [UIImage imageNamed:[NSString stringWithFormat:#"a.jpg"]];
imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448);
imgView.userInteractionEnabled = YES;
[self.view imgView];
- (void)panAction : (UIPanGestureRecognizer *)sender {
CGPoint CGP = imgView.center;
if(newScale != 1.0 && CGP.x-160 != 0 && CGP.y-224 != 50){
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(imgView.center.x + p.x, imgView.center.y + p.y);
imgView.center = movedPoint;
[sender setTranslation:CGPointZero inView:self.view];
}
}
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
newScale = 1 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
You must use the following condition check in your Pan Gesture action:
For you information, as you move the object, this panAction() method is called regularly at each pixel move.
- (void)panAction : (UIPanGestureRecognizer *)sender {
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
//Do something whatever you need to be done while the pan gesture starts
}
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged)
{
//Do something whatever you need to be done while the imageView object is in the moving state
}
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded){
//Do something you need to do as you end the pan gesture.
}
}
I need to increase and decrease GPUImagePinchDistortionFilter radius according to pinch, I am using pinch gesture recognizer and storing the starting scale of the gesture recognizer. I need to calculate the pinch scale between -2.0 and 2.0. Here is my code.
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
CGFloat kMaxScale = 2.0;
CGFloat kMinScale = -2.0;
CGFloat newScale = 1.0 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
[pinchDistortionFilter setRadius:newScale];
}
I have followed some posts to do this. but still stuck, also I need to set this every time the user pinches but the radius must be within the min and max. actually I am confused how to achieve this, can anyone help me with this ?
Thank You.
I think you should try to save the lastScale and radius too in correct place, and the radius should be > 0 see my answer below for my implementation, it might help you.
float currentApplicableScale = (gestureRecognizer.scale/2);
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
lastRadius = lastScale;
}else if (gestureRecognizer.state==UIGestureRecognizerStateBegan) {
firstScale = currentApplicableScale;
}else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
float radius = currentApplicableScale;
if (lastRadius<=0) {
lastRadius = 0.25;
}
radius = radius - (firstScale - lastRadius);
if (radius>0 && radius < 0.5) {
[(GPUImagePinchDistortionFilter*)pinchFilter setRadius:radius];
lastScale = radius; //Save here
}
}
I've created a UIGestureRecognizer much like this one:
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
This works as expected, however my client wants it to be less sensitive to touch. How can I reduce the velocity of the pinching (both inward and outward) so that it zooms at about 80% the default velocity?
Did you tried to scale the current value to the 80%.
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat maxScale = 2.0;
CGFloat currentScale = [gestureRecognizer scale];
currentScale = 0.8 * currentScale; //80% of scaling
if(currentScale < 0.8)
currentScale = 0.8;
if(currentScale > maxScale)
currentScale = maxScale;
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], currentScale, currentScale);
}
Try this.
- (void)handlePinching:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
if (CGRectIsEmpty(self.initalFrame)) {
self.initalFrame = gestureRecognizer.view.frame;
// store view's original frame and never change it
}
if (self.preScale == 0.f) {
self.preScale = 1.f;
}
gestureRecognizer.scale = self.preScale;
// gestureRecognizer and view should share the same scale state at the beginning
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = gestureRecognizer.scale;
newScale = (newScale - self.preScale) * 0.8 + self.preScale;
newScale = MIN(newScale, kMaxScale);
newScale = MAX(newScale, kMinScale);
CGRect newFrame = self.initalFrame;
newFrame.size.height *= newScale;
newFrame.size.width *= newScale;
gestureRecognizer.view.frame = newFrame;
self.preScale = newScale;
}
}
The points are
use frame to implement scale.
do change to the scale variable's change to slow/speed scaling.
Turns out the excessive speed of the zoom was actually a bug in the linked answer's code. Sadly this doesn't actually answer my actual question though (for which I'd still like an answer!), but it does serve to solve my client's problem.
Note the added line at the bottom that resets the gestureRecognizer back to 1:
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
...
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
gestureRecognizer.scale = 1;
// ^ added this line
}
}
I'm using UIPinchGestureRecognizer in my app to zoom in on a view (and yes, there's a reason I'm not using UIScrollView). When I pinch outwards with my fingers, the view zooms in as expected, and if I then reverse pinch without taking my fingers off the screen, it also zooms right. However, if I initiate the zoom by pinching inwards, the rate at which the view zooms is dramatically slower. I'm guessing this is because of how UIPinchGestureRecognizer works - the scale of the UIPinchGestureRecognizer is >1 when pinching outwards, and <1 when pinching inwards. Unfortunately, I do not know how to accurately reflect this in my code.
- (IBAction)didDetectPinchGesture:(id)sender {
UIPinchGestureRecognizer *gestureRecognizer = (UIPinchGestureRecognizer *)sender;
CGFloat scale = [gestureRecognizer scale];
switch ([gestureRecognizer state]) {
case UIGestureRecognizerStateBegan:
_lastScale = [gestureRecognizer scale];
break;
case UIGestureRecognizerStateChanged:
CGFloat currentScale = [[self.imageView.layer valueForKeyPath:#"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 5.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (_lastScale - scale); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
NSLog(#"%f", newScale);
CGAffineTransform transform = CGAffineTransformScale([self.imageView transform], newScale, newScale);
self.imageView.transform = transform;
_lastScale = scale; // Store the previous scale factor for the next pinch gesture call
break;
default:
_lastScale = [gestureRecognizer scale];
break;
}
}
A very simple solution to this is to reset the gestureRecognizer scale back to 1 when you're finished:
...
default:
_lastScale = [gestureRecognizer scale];
// Add this:
[gestureRecognizer setScale:1];
break;
}