你好开发(这是我在堆栈流行流中的第一篇文章,所以请在评论中告诉我任何我做错了什么:)。
此代码检测用户是否夹住:
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以显示更多图像"。我正在使用图像缩略图,如果用户捏住,它将显示更多图像。感谢您的帮助,并度过一个愉快的假期。
启动一个具有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];
}