在 xCode 对象 C 中实现滑动手势



我在目标 c 中编程相当新手,并且正在尝试实现滑动手势以在我在 xcode 中创建的应用程序上的视图控制器之间滑动。我试图使它,当我向左滑动时,它会切换到另一个我命名为"SecondViewController"的视图控制器。我已经在我的 .h 文件中为我的手势创建了出口和操作,在我的 .m 文件中,我添加了以下代码:

- (IBAction)swipeLeft:(id)sender {
ViewController *SecondViewController = [[ViewController alloc] init];
[self presentViewController:SecondViewController animated:YES 
completion:nil];

每当我运行该应用程序时,滑动时没有任何反应。是否有我尚未完成的工作需要做一些事情?

基本上有四种滑动手势可用:

UISwipeGestureRecognizerDirectionRight
UISwipeGestureRecognizerDirectionLeft
UISwipeGestureRecognizerDirectionUp  
UISwipeGestureRecognizerDirectionDown

您可以根据需要使用其中任何一个。要添加上述任何手势,您可以分配手势,然后将其添加到您的特定视图中。因此,一旦您滑动检测到,它就会调用相关方法。

例如,对于左右手势:

UISwipeGestureRecognizer *gestureRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerRight:)];
[gestureRecognizerRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:gestureRecognizerRight];
UISwipeGestureRecognizer *gestureRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandlerLeft:)];
[gestureRecognizerLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:gestureRecognizerLeft];
-(void)swipeHandlerRight:(id)sender
{
}
-(void)swipeHandlerLeft:(id)sender
{
}

最新更新