简体   繁体   中英

UISwipeGestureRecognizer swipe gesture in iOS 3.2

I see in the documentation that UISwipeGestureRecognizer is available in iOS 3.2 and later. I was planning to use this to detect swipe gestures in my Application.

What would be the consequences of running my application in an older iOS say 3.1.3 if I do implement UISwipeGestureRecognizer ?

If you code for backwards compatibility, meaning you check that a class or method exists before you use it, then pre 3.2 users would simply not be able to swipe. Otherwise you should mark your application as requiring 3.2 or later to run.

Class c = NSClassFromString( @"UISwipeGestureRecognizer" );

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

I discovered that for compatibility with 3.1.3...

The check for the class "UISwipeGestureRecognizer" was not sufficient.

I finally decided on a quick fix which checks the version (Even though I'm not 100% happy with it):

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

GestureRecognizers仅适用于> = iOS 3.2,因此无论如何都不能在iOS 3.1.3中使用它们。

The Apple documentation says that it is only available in iOS 3.2 and later, but this is not the whole story! Code which uses UISwipeGestureRecognizer compiles without an error or warning when "iPhone OS Deployment Target" is 3.1.3, and it works fine on my 3.1.3 device.

I guess that before 3.2 UISwipeGestureRecognizer was considered "undocumented API".

That's true that before 3.2 UISwipeGestureRecognizer is compiled without warning or errors as stated before but however, I experienced a problem about that. My app was compiled but when I run my app in a 3.1 iphone, UISwipeGestureRecognizer was detecting a swipe event twice. So, I made some conditional coding. My implementation:

- (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];
}

and in another method, let's say in viewdidload

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 3.2){
   UISwipeGestureRecognizer  *swipe = [[UISwipeGestureRecognizer  alloc]
                                         initWithTarget:self action:@selector(handleSwipe)];

        [self addGestureRecognizer:swipe];
    }

This implementation saves you from the risk of using a private api, although it is not private now. Also, it eliminates the duplicate swipe event problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM