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

前端之家收集整理的这篇文章主要介绍了如何检测用户是否在iOS编程中没有捏合和缩放前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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秒内没有收缩,因此我可以提醒用户@“捏合以显示更多图像”.我正在使用图像缩略图,如果用户捏它将显示更多图像.感谢您的帮助,祝您度过愉快的假期.

解决方法

使用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];
}

猜你在找的Xcode相关文章