0

Possible Duplicate:
Using UIPinchGestureRecognizer to scale uiviews in single direction

My code is below:

 UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(croperViewScale:)];
        croperViewGessture.delegate=self;
        [croperView addGestureRecognizer:croperViewGessture];   

     -(void)CanvasScale:(id)sender
{
    if([(UIPinchGestureRecognizer *)sender state]==UIGestureRecognizerStateBegan)
    {
        if ([sender numberOfTouches] == 2) {
            _pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
            _pntOrig[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
        } else {
            _pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationInView:cropedAngle];
            _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;
    }
    if ([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged) {
        if ([sender numberOfTouches] == 2) {
            CGPoint pntNew[2];
            pntNew[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
            pntNew[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];

            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 = [(UIPinchGestureRecognizer *)sender scale];
            CGFloat dscale = scale - 1.0;
            _xScale = dscale * pX + 1;
            _yScale = dscale * pY + 1;
        }
    }
CGAffineTransform transform = cropedAngle.transform;
CGAffineTransform newTarnsform = CGAffineTransformScale(transform, _lenOrigX, _lenOrigY);
[cropedAngle setTransform:newTarnsform];

}

But problem is that when I do Zoomin OR Zoomout then view spread on all over the screen and after it disable Please view my code and tell me what is wrong .

Please help me in this issue i am Thankfull in advance.

Community
  • 1
  • 1
Bilal hussain
  • 105
  • 1
  • 3
  • 9
  • you have to implement the code in `croperViewScale:` not `CanvasScale:` or change this line `UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(croperViewScale:)];` to `UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(CanvasScale:)];` – Pratyusha Terli Dec 01 '12 at 11:39
  • Sory, both or same i mean to say both are the same functions name my funtion name is canvasScale i have to implment now correct place – Bilal hussain Dec 01 '12 at 11:45
  • sorry,I cannot found any specific solution with this please give me any other way to solve that problem i have already so much time spent on this issue otherwise thanks you for giving time.............Best regards – Bilal hussain Dec 03 '12 at 09:40

2 Answers2

2

I wrote my own custom extension to UIPinchGestureRecognizer to provide an xScale and a yScale, in addition to the normal scale. This is a drop in replacement for UIPinchGestureRecognizer. You now have the option of looking at the normal scale or the new xScale and yScale. Use these values accordingly to scale your view based on how the user does the pinch gesture.

RMPinchGestureRecognizer.h

#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@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

RMPinchGestureRecognizer.m

#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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • @ Thanks Rmaddy: i am new in ios please can u tell me how to use it in my code which i show above. – Bilal hussain Dec 01 '12 at 07:01
  • +1. Liked this solution compared to the one here http://stackoverflow.com/questions/6759028/using-uipinchgesture-to-scale-uiviews-in-single-direction – iDev Dec 01 '12 at 23:48
  • 1
    @ACB Thanks. That other solution isn't very reusable. – rmaddy Dec 01 '12 at 23:59
0

Take a look HERE

Your line CGAffineTransformScale(transform, scaleTemp,scaleTemp); uses the same scaleTemp variable to modify both the x and y values of the transform.

Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
  • Thnaks Adam ! When I cahnge here then, The view disable and after it not show so please give me any example that solve my issue – Bilal hussain Dec 01 '12 at 06:54
  • You'll need to do some calculations. A similar question has been asked here: http://stackoverflow.com/questions/6759028/using-uipinchgesture-to-scale-uiviews-in-single-direction – Adam Johnson Dec 01 '12 at 06:59
  • this question is also same problem, When i use it and try to solve then it become again in my problem first it only rotate in y direction after solution it also increase both height and width not seprately that my problem OTherwise thanks for giving time have you any other solution so please tell me....................Thanks – Bilal hussain Dec 03 '12 at 09:44
  • Every one that will do this problem use this tutorial[1]: https://github.com/spoletto/SPUserResizableView – Bilal hussain Dec 04 '12 at 06:44