organgrinder
organgrinder

Reputation: 11

UIPinchGestureRecognizer not working in Xcode/iPhone simulator

Working in Xcode/iPhone simulator, I have an app where UIGestureRecognizers were working for pinch, tap, and pan. Without changing any code, pinches are no longer being recognized (I am holding down the option key while moving the mouse to show the two grey circles moving together or apart). Pan and tap still both work. Has anyone else experienced this problem?

It appears that something is wrong with the recognizer itself or the simulator because the code below for pinching never gets called, but it works for for panning.

- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
NSLog(@"in pinch method");
if ((gesture.state == UIGestureRecognizerStateChanged) ||
    (gesture.state == UIGestureRecognizerStateEnded)) {
    self.scale *= gesture.scale; // adjust our scale
    gesture.scale = 1;           // reset gestures scale so future changes are incremental
}
}

- (void)tap:(UITapGestureRecognizer *)gesture
{
NSLog(@"in tap method");
if (gesture.state == UIGestureRecognizerStateEnded) 
    self.originInPixels = [gesture locationInView:self];
}

I tried creating a new app with a simple MKMapView, which loads the map properly, and pan and tap work - but pinch still doesn't work.

I'm working in iOS 5.1.

Any ideas?

Upvotes: 0

Views: 2517

Answers (3)

natmat
natmat

Reputation: 21

I had the same problem on my macbook air trackpad with XCode 4.5.2 thinking that [option]+[single finger touch movement] would do a pinch (gray balls moving in/out).

However, you need to apply [option]+[3-finger touch] for it to work.

This works in simulator, but annoyingly is no help when running on connected iOS device. Though my downloaded app works, running with iPhone connected to XCode doesn't, agh!

Upvotes: 0

organgrinder
organgrinder

Reputation: 11

The UIGestureRecognizer began working again on its own, and I'm still not sure why. It may have been as simple as I wasn't holding down the mouse button while also holding down the option key, but I thought I tried that before.

Thanks for the suggestions above.

Upvotes: 1

Ravindhiran
Ravindhiran

Reputation: 5384

Please try this

include this code to your project

example.h

CGFloat     lastScale;
CGPoint     lastPoint;

example.m

-(void)viewDidLoad {

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    [self addGestureRecognizer:pinchGesture];
    [pinchGesture release];

}

-(void) handlePinch:(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];
    NSLog(@"%f",currentScale);
    // Constants to adjust the max/min values of zoom
    const CGFloat kMaxScale = 3.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);
    NSLog(@"%f",lastScale);
    [gestureRecognizer view].transform = transform;

    lastScale = [gestureRecognizer scale];
}

}

Upvotes: 1

Related Questions