0

I would like to create an action "swipe gesture up & down" differente in portrait and landscape mode, but i'm newbies I can't do this, can you please help me?

Here is my code :

-(void)swipeToggle:(id)sender {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
    if (UISwipeGestureRecognizerDirectionUp){

         NSLog(@"if gesture up - LS");


    } else if (UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"else if gesture down - LS");

     }

}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
    if (UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"if gesture down - PT");

    }
    else if (UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"else if gesture up - PT");


    }

}

}

4

1 回答 1

0

You need to add this to your view:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToggle:)];

swipe1.direction = UISwipeGestureRecognizerDirectionUp ;
[self.view addGestureRecognizer:swipe1];
[swipe1 release];

UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeToggle:)];

swipe2.direction = UISwipeGestureRecognizerDirectionDown ;
[self.view addGestureRecognizer:swipe2];
[swipe2 release];
}

then your toggle:

-(void)swipeToggle:(UISwipeGestureRecognizer *)sender
{ 
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
{
    if (sender.direction ==  UISwipeGestureRecognizerDirectionUp){

        NSLog(@"if gesture up - LS");


    } else if (sender.direction ==  UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"else if gesture down - LS");

    }

}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{

    if (sender.direction == UISwipeGestureRecognizerDirectionDown)
    {

        NSLog(@"if gesture down - PT");

    }
    else if ( sender.direction == UISwipeGestureRecognizerDirectionUp)
    {

        NSLog(@"else if gesture up - PT");


    }


}
}

Unfortuantelly you need to create 2 swipe gesture recognizers because setting the swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp; will create a bitwise operation and the direction won't be detected.

于 2013-07-06T17:13:50.370 回答