2010-07-10 202 views
1

我在文档中看到UISwipeGestureRecognizer在iOS 3.2及更高版本中可用。我打算用这个来检测我的应用程序中的滑动手势。iOS 3.2中的UISwipeGestureRecognizer轻扫手势

如果我执行UISwipeGestureRecognizer,那么在旧iOS中运行我的应用程序会导致什么后果?

回答

3

如果您使用代码向后兼容,即在使用它之前检查是否存在类或方法,那么3.2之前的用户根本无法刷卡。否则,您应该将您的应用程序标记为要求3.2或更高版本运行。

Class c = NSClassFromString(@"UISwipeGestureRecognizer"); 

if (c) { 
    UISwipeGestureRecognizer *recognizer = [[c alloc] init]; 
} else { 
    // pre 3.2 do something else 
} 
+0

也...对于iOS 3.1.x的兼容性,请记住检查特定的子类“UISwipeGestureRecognizer”,而不仅仅是“UIGestureRecognizer”。这是因为UIGestureRecognizer确实存在于iOS 3.2之前,而UISwipeGestureRecognizer刚刚在iOS 3.2中引入。 – paiego 2011-08-01 22:44:24

+0

这不适合我。 UISwipeGestureRecognizer存在于iOS 3.1中(在真实设备上测试过)。虽然它存在,但它不适用于iOS 3.1。它不响应选择器“方向”或“委托”。但是,由于一些神秘原因,alloc和initWithTarget:action:适用于iOS 3.1。我通过按照paiego的建议测试版本号来解决这个问题。 – 2012-04-06 16:45:54

0

手势识别器仅在> = iOS 3.2中可用,所以无论如何您都不能在iOS 3.1.3中使用它们。

+1

是否意味着人们四处 Rupert 2010-07-10 17:06:07

0

苹果文档说,它只在iOS 3.2和更高版本中可用,但这不是全部内容!当“iPhone操作系统部署目标”是3.1.3时,使用UISwipeGestureRecognizer的代码无错误或警告编译,并且它在我的3.1.3设备上正常工作。

我猜在3.2之前UISwipeGestureRecognizer被认为是“无证API”。

0

确实如此,在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的风险,节省了你说的,虽然现在不是私人的。此外,它消除了重复刷卡事件问题。

1

我发现,可以兼容3.1.3 ...

为类“UISwipeGestureRecognizer”的检查是不够的。

我终于决定速战速决,检查版本(虽然我不喜欢它100%):

+ (BOOL)isVersionSwipeable 
{ 
    float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
    return (version >= 3.2); 
} 
+0

虽然理论上更正确地检查类可用性然后版本号,在这种特殊情况下检查类可用性没有得到积极的结果 - UISwipeGestureRecognizer类存在于iOS 3.1(在真实设备上测试)。 – 2012-04-06 16:42:36

+0

此外,上述代码返回版本3.1的3.0999999。我会建议测试'version> 3.19'而不是'version> = 3.2'。 – 2012-04-06 16:49:02