[英]UISwipeGestureRecognizer swipe gesture in iOS 3.2
我在文档中看到UISwipeGestureRecognizer
在iOS 3.2及更高版本中可用。 我打算用它来检测我的应用程序中的滑动手势。
如果我实现了UISwipeGestureRecognizer
那么在旧版iOS中运行我的应用程序会产生什么后果呢?
如果您编写了向后兼容性代码,这意味着在使用它之前检查某个类或方法是否存在,那么3.2之前的用户根本无法刷卡。 否则,您应将应用程序标记为要求运行3.2或更高版本。
Class c = NSClassFromString( @"UISwipeGestureRecognizer" );
if ( c ) {
UISwipeGestureRecognizer *recognizer = [[c alloc] init];
} else {
// pre 3.2 do something else
}
我发现为了兼容3.1.3 ......
检查“UISwipeGestureRecognizer”类是不够的。
我终于决定快速修复它来检查版本(即使我对它不是100%满意):
+ (BOOL)isVersionSwipeable
{
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
return (version >= 3.2);
}
GestureRecognizers仅适用于> = iOS 3.2,因此无论如何都不能在iOS 3.1.3中使用它们。
Apple文档说它仅在iOS 3.2及更高版本中可用,但这不是全部故事! 当“iPhone OS部署目标”为3.1.3时,使用UISwipeGestureRecognizer的代码编译时没有错误或警告,并且它在我的3.1.3设备上正常工作。
我想在3.2之前,UISwipeGestureRecognizer被认为是“未记录的API”。
确实如此,在3.2之前编译UISwipeGestureRecognizer没有警告或错误,但是,我遇到了一个问题。 我的应用程序已经编译但是当我在3.1 iphone中运行我的应用程序时,UISwipeGestureRecognizer正在检测两次刷卡事件。 所以,我做了一些条件编码。 我的实施:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version < 3.2) {
UITouch *touch = [touches anyObject];
startPosition = [touch locationInView:self];
}
[super touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version < 3.2){
UITouch *touch = [touches anyObject];
CGPoint endPosition = [touch locationInView:self];
if (endPosition.x-startPosition.x>30) { //difference between end and start must be min. 30 pixels to be considered as a swipe. if you change it as startPosition.x-endPosition.x you could detect left swipe
[self handleSwipe]; //my swipe handler
}
}
[super touchesEnded:touches withEvent:event];
}
在另一种方法中,让我们说在viewdidload中
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.2){
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipe)];
[self addGestureRecognizer:swipe];
}
此实现使您免于使用私有API的风险,尽管它现在不是私有的。 此外,它消除了重复的滑动事件问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.