添加滑动手势,无法滑动

时间:2016-08-17 06:32:22

标签: ios objective-c uiswipegesturerecognizer

这里我有三个视图,一个是左,中,右,所以我想添加滑动功能,我尝试了以下方式,但仍然无法制作它。我怎么能这样做。

代码:

  - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }


    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];

        UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
        [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
        [self.view addGestureRecognizer:gestureRecognizer];
        //Left Swipe

        UISwipeGestureRecognizer *gestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)];
        [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
        [self.view addGestureRecognizer:gestureRecognizer2];

    }

正确的过渡

    -(void)swipeHandlerRight:(id)sender
    {

         RightViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"];


CATransition* transition = [CATransition animation];
    transition.duration = 0;
    transition.type = kCATransitionFromRight;
   // transition.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:transition forKey:kCATransition];
    [self presentViewController:videoScreen animated:NO completion:nil];


    }

左转移

    -(void)swipeHandlerLeft:(id)sender
    {
        LeftViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"];
         CATransition* transition = [CATransition animation];
    transition.duration = 0;
    transition.type = kCATransitionFromLeft;
    // transition.subtype = kCATransitionFromRight;
    [self.view.window.layer addAnimation:transition forKey:kCATransition];


    }

2 个答案:

答案 0 :(得分:0)

替换此

[self.navigationController pushViewController:videoScreen animated:YES];

通过这个

[self presentViewController:videoScreen animated:YES completion:NULL];

它将使您的视图控制器出现。但由于左视图控制器或右视图控制器没有手势识别器,因此您不会从这些视图控制器返回。为此,您可以让它们像滑动一样。

答案 1 :(得分:0)

我得到了解决方案兄弟。我为你的问题尝试了示例项目。现在我得到了解决方案。

首先我在Storyboard中设置ViewControllers。在下面的屏幕截图中查看具有UINavigationController,ViewController,LeftViewController,RightViewController的故事板。

enter image description here

然后在ViewController.m

#import "ViewController.h"
#import "RightViewController.h"
#import "LeftViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning 
{
   [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];

  //swipe right
  UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
  [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
  [self.view addGestureRecognizer:gestureRecognizer];

  //swipe left
  UISwipeGestureRecognizer *gestureRecognizer2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)];
  [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
  [self.view addGestureRecognizer:gestureRecognizer2];

}

#pragma mark - swipe right function
-(void)swipeHandlerRight:(id)sender
{
   RightViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"RightViewController"];

   CATransition *animationRight = [CATransition animation];
   [animationRight setDuration:0];
   [animationRight setType:kCATransitionFromRight];
   [animationRight setSubtype:kCATransitionFromRight];
   [animationRight setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
   [self.view.window.layer addAnimation:animationRight forKey:kCATransition];
   [self presentViewController:videoScreen animated:NO completion:nil];
}

#pragma mark - swipe left function
-(void)swipeHandlerLeft:(id)sender
{
   LeftViewController *videoScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"LeftViewController"];
   CATransition *animationLeft = [CATransition animation];
   [animationLeft setDuration:0];
   [animationLeft setType:kCATransitionFromLeft];
   [animationLeft setSubtype:kCATransitionFromLeft];
   [animationLeft setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
   [self.view.window.layer addAnimation:animationLeft forKey:kCATransition];
   [self presentViewController:videoScreen animated:NO completion:nil];
}

RightViewController.m

#import "RightViewController.h"

@interface RightViewController ()

@end

@implementation RightViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning 
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

#pragma mark - Back to MiddleView
- (IBAction)actionBackToMiddleView:(id)sender
{
   [self dismissViewControllerAnimated:YES completion:nil];
}
@end

LeftViewController.m

#import "LeftViewController.h"

@interface LeftViewController ()

@end

@implementation LeftViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning 
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

#pragma mark - Back to MiddleView
- (IBAction)actionBackToMiddleViewFromLeftView:(id)sender
{
   [self dismissViewControllerAnimated:YES completion:nil];
}
@end