【问题标题】:Adding swipe gesture, Unable to swipe添加滑动手势,无法滑动
【发布时间】:2016-08-17 06:32:21
【问题描述】:

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

代码:

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


    }

【问题讨论】:

  • 您想要实现的目标,我认为您应该使用页面视图控制器或在启用分页的滚动视图中放置 3 个视图。根据您的要求使滚动视图宽度尽可能大。
  • 这些方法一次都没有调用吗?
  • @NileshJha 当我添加断点并运行程序时,视图将被调用,当我向左滑动时,断点转到 leftViewController 和右侧相同,但中间标签仍然显示在那里,当我在各个屏幕上添加标签时向左滑动时,它应该显示左侧标签
  • 你的控制器有 UINavigationController 作为根视图控制器吗?
  • Virat 检查我的答案

标签: ios objective-c uiswipegesturerecognizer


【解决方案1】:

替换这个

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

由此

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

它会让你的视图控制器出现。但是由于左视图控制器或右视图控制器没有手势识别器,因此您不会从那些视图控制器返回。为此,您必须让它们喜欢滑动。

【讨论】:

  • 是的,这是可行的,但我希望动画应该根据滑动显示,但它从底部呈现左视图控制器,但它应该来自左侧
  • @virat 你是否添加了 UINavigationControl‌​ler?
  • @iRaju 不,我没有添加
  • 在根视图控制器的情节提要中添加
  • @SMi ya 它现在正在工作,但我想从右边回到中间视图如何从右边回到中间。
【解决方案2】:

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

首先我在storyboard中设置了ViewControllers。在下面的截图中查看包含UINavigationController、ViewController、LeftViewController、RightViewController的storyboard。

然后在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多