0

Using xCode 4.3.2, I have the following code in initWithFrame in my view.m:

 UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc]
            initWithTarget: self action: @selector(pinch:)
 ];

 oldScale = recognizer.scale;
 [self pinch: recognizer];
 [self addGestureRecognizer: recognizer];

 // code to create label is also in here, works.  label displays no problem

 [self addSubview: label];

my pinch method is as follows:

- (void) pinch: (UIPinchGestureRecognizer *) recognizer
{
    NSLog(@"pinch:");
    label.bounds = CGRectMake(
    (self.bounds.size.width - width * recognizer.scale) / 2,
    (self.bounds.size.height - height * recognizer.scale) / 2,
    width * recognizer.scale,
    height * recognizer.scale
    );

    label.font = [UIFont systemFontOfSize: 20 * recognizer.scale];
    NSString *verdict;

    if (recognizer.scale > oldScale) {
        verdict = @"spread";
    } else if (recognizer.scale < oldScale) {
        verdict = @"pinch";
    } else {
        verdict = @"neither";
    }
    oldScale = recognizer.scale;

    label.text = [NSString stringWithFormat: @"%@ %g",
                 verdict, recognizer.scale
    ];
}

because i actually call pinch in my initWithFrame method, it runs once, but when i perform a pinch in the iphone simulator, it doesn't register at all. is there some setting in xcode 4.3.2 i don't know about? this code works everywhere else i've tried running it - but those versions of xcode are all 4.3.

2 Answers2

1

Add UIGestureRecognizerDelegate in .h file

Use following code in .m file in view did load...

// Gesture Reconizer Delegate Methods
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
 }

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  return  YES;
 }

 -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
   return  YES;
 }



UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(checkpinch)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.ScrollView addGestureRecognizer:pinch];
[pinch release];

// Gesture Reconizer Methods

-(void)checkpinch {    

 NSLog(@"YES");
}

>>See this Edited..

Pinch can detect on scroll-view if you want to detect on view than see following reference...

Detect pinch on UIView

Hope, this will help you...

Nitin
  • 7,455
  • 2
  • 32
  • 51
  • Hmm.. I made my ViewController.h And put the delegate methods in the ViewController.m file. In my View.m file, in initWithFrame: controller: method, I make my viewcontroller the delegate like so: [recognizer setDelegate: viewController]; ... still nothing. if i actually call the method pinch: ... – Script This Apr 27 '12 at 20:43
  • I am so lost. I can make the simulator respond to taps, swipes, double taps etc. Just not pinch or RotationGesture. is there a setting I'm forgetting in 4.3.2? – Script This Apr 27 '12 at 21:12
  • I tested my ans and it's working properly...did you add delegates method which returns yes... – Nitin Apr 28 '12 at 02:10
  • I think something unusual is happening. I cannot get either approach to work in 4.3.2. Thanks for your help/input. When I figure this out, I'll let you know. – Script This Apr 28 '12 at 03:22
  • Nit, Are you running Lion with Xcode 4.3.2? I have tried to run the original code in 4.2, 4.3 and 4.3.2 on 3 different computers at home. I can't make it work anywhere. I've tried to run my updated code (using your advice/code above) and I've tried running the code at the bottom of this page http://iphonedevelopertips.com/event-handling/gestures-recognizers-tap-pinchzoom-rotate-swipe-pan-long-press.html it recognizes everything except pinch. I really appreciate the time you've taken already on this. Much appreciated. – Script This Apr 29 '12 at 01:03
  • I have lion with xcode 4.1 and 4.2.3 and tested it and it's working very well. Did you try with scroll view..? – Nitin Apr 29 '12 at 03:30
0

Maybe I am oversimplifying this, but wouldn't be easier to: 1) In your VC, create an IBoutlet property to an instance of your view 2) connect that IBOutlet to your view 3) setup your UIPinchGetsureRecognizer in the VC by overriding the view setter method and change target to the view and keep the pinch method in the view. This way the UIPinchGetsureRecognizer will load every time the view is loaded and be ready for a pinch. Then you would not need to call the pinch method (which defeats the purpose). KB

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
  • This is an example from a class that works in Xcode 4.3 but not 4.3.2. Not something I'm writing from scratch. The purpose was to illustrate how to create and use a pinch gesture recogizer. We haven't gotten to IBOutlets yet. :) – Script This Apr 27 '12 at 20:36