iProgrammed
iProgrammed

Reputation: 980

How to detect if User isn't Pinching and Zooming In iOS Programming

Hello Devs (this is my first post in Stack-Overflow so please tell me anything in the comment that i did wrong :).

This code detect if the the user is Pinching:

UIPinchGestureRecognizer *twoFingerPinch = 
  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];

The Void Action:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
  NSLog(@"Pinch scale: %f", recognizer.scale);
}

The issue is that I want to detect if the user hasn't pinched in under 20 seconds, so I can alert the user that @"Pinch to show more Images". I am using image Thumbnails and if the user pinches it will display more images. Thanks for your Help, and Have a great Holiday.

Upvotes: 3

Views: 530

Answers (1)

iDev
iDev

Reputation: 23278

Start a timer with 20 seconds which will be invalidated only in twoFingerPinch method when the user pinches. Start this timer whenever you need to start checking for this. In the timer action method you can put the code to show this alert.

Declare timer in .h file,

@property(nonatomic, strong) NSTimer *timer;

In viewDidLoad or whichever method you want to start the timer for checking this,

self.timer = [NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(showAlert) userInfo:nil repeats:YES];

In showAlert method,

- (void)showAlert {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Pinch to show more Images" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
}

In twoFingerPinch method,

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{
  NSLog(@"Pinch scale: %f", recognizer.scale);
  [self.timer invalidate];

  //if the timer needs to be restarted add,
  self.timer = [NSTimer scheduledTimerWithTimeInterval:20.0f target:self selector:@selector(showAlert) userInfo:nil repeats:YES];
}

Upvotes: 3

Related Questions