4

你好 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 秒内没有捏合,所以我可以提醒用户 @"Pinch to show more Images"。我正在使用图像缩略图,如果用户捏它会显示更多图像。感谢您的帮助,祝您假期愉快。

4

1 回答 1

3

twoFingerPinch启动一个 20 秒的计时器,只有在用户捏合时才会失效。每当您需要开始检查时启动此计时器。在计时器操作方法中,您可以放置​​代码以显示此警报。

在 .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];
}
于 2012-12-25T02:55:28.003 回答