简体   繁体   English

轻扫手势可进入LS模式和人像模式

[英]Swipe Gesture for LS Mode & Portrait Mode

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


    }

}

} }

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; 不幸的是,您需要创建2个滑动手势识别器,因为设置了swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp; swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp; will create a bitwise operation and the direction won't be detected. 将创建按位运算,并且不会检测到方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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