用于LS模式的滑动手势&肖像模式

时间:2013-07-06 16:12:01

标签: ios swipe gesture

我想在纵向和横向模式下创建一个“滑动手势向上和向下”不同的动作,但我是新手我不能这样做,你能帮我吗?

这是我的代码:

-(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");


    }

}

}

1 个答案:

答案 0 :(得分:0)

您需要将此添加到您的视图中:

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

然后你的切换:

-(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");


    }


}
}

不幸的是,您需要创建2个滑动手势识别器,因为设置swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp; 将创建按位操作,并且不会检测到方向。