Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wrote my own custom extension to <code>UIPinchGestureRecognizer</code> to provide an <code>xScale</code> and a <code>yScale</code>, in addition to the normal <code>scale</code>. This is a drop in replacement for <code>UIPinchGestureRecognizer</code>. You now have the option of looking at the normal <code>scale</code> or the new <code>xScale</code> and <code>yScale</code>. Use these values accordingly to scale your view based on how the user does the pinch gesture.</p> <p>RMPinchGestureRecognizer.h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;UIKit/UIGestureRecognizerSubclass.h&gt; @interface RMPinchGestureRecognizer : UIPinchGestureRecognizer { CGPoint _pntOrig[2]; CGFloat _lenOrigX; CGFloat _lenOrigY; CGFloat _xScale; CGFloat _yScale; } @property (nonatomic, readonly) CGFloat xScale; @property (nonatomic, readonly) CGFloat yScale; @end </code></pre> <p>RMPinchGestureRecognizer.m</p> <pre><code>#import "RMPinchGestureRecognizer.h" @implementation RMPinchGestureRecognizer @synthesize xScale = _xScale; @synthesize yScale = _yScale; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; if (self.state == UIGestureRecognizerStateChanged) { if ([self numberOfTouches] == 2) { CGPoint pntNew[2]; pntNew[0] = [self locationOfTouch:0 inView:self.view]; pntNew[1] = [self locationOfTouch:1 inView:self.view]; CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x); CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y); CGFloat dX = fabs(lenX - _lenOrigX); CGFloat dY = fabs(lenY - _lenOrigY); CGFloat tot = dX + dY; CGFloat pX = dX / tot; CGFloat pY = dY / tot; CGFloat scale = [self scale]; CGFloat dscale = scale - 1.0; _xScale = dscale * pX + 1; _yScale = dscale * pY + 1; } } } - (void)setState:(UIGestureRecognizerState)state { if (state == UIGestureRecognizerStateBegan) { if ([self numberOfTouches] == 2) { _pntOrig[0] = [self locationOfTouch:0 inView:self.view]; _pntOrig[1] = [self locationOfTouch:1 inView:self.view]; } else { _pntOrig[0] = [self locationInView:self.view]; _pntOrig[1] = _pntOrig[0]; } _lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x); _lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y); _xScale = 1.0; _yScale = 1.0; } [super setState:state]; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload