如何检测用户是否在iOS编程中没有捏合和缩放

时间:2012-12-25 02:39:08

标签: ios xcode ios6 uigesturerecognizer gestures

Hello Devs(这是我在Stack-Overflow中的第一篇文章所以请在评论中告诉我我做错了:)。

此代码检测用户是否正在捏合:

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

虚空行动:

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

问题是我想检测用户是否在20秒内没有收缩,因此我可以提醒用户@“捏以显示更多图像”。我正在使用图像缩略图,如果用户捏它将显示更多图像。感谢您的帮助,祝您度过愉快的假期。

1 个答案:

答案 0 :(得分:3)

启动一个20秒的计时器,当用户捏住时,该计时器仅在twoFingerPinch方法中无效。无论何时需要开始检查,都要启动此计时器。在计时器操作方法中,您可以输入代码以显示此警报。

在.h文件中声明计时器,

@property(nonatomic, strong) NSTimer *timer;

viewDidLoad中,或者您想要启动计时器以进行检查的方法,

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

showAlert方法中,

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

twoFingerPinch方法中,

- (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];
}