通过滑动手势实现视图导航

19

如何使用滑动手势在视图之间垂直切换?


你知道如何在安卓系统中实现相同的功能吗? - Prabs
这可能是你正在寻找的东西:https://dev59.com/T2Mm5IYBdhLWcg3whfae#17540632 - Tony
5个回答

29
我找到了答案。 我将代码发布供您参考。 谢谢 :-)

viewDidLoad

  UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreendown:)] autorelease];
  swipeGesture.numberOfTouchesRequired = 1;
  swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
  [m_pImageView addGestureRecognizer:swipeGesture];

现在

- (void)swipedScreendown:(UISwipeGestureRecognizer*) swipeGesture {
  m_pViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
  CATransition *transition = [CATransition animation];
  transition.duration = 0.75;
  transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  transition.type = kCATransitionPush;
  transition.subtype = kCATransitionFromBottom;
  transition.delegate = self;
  [self.view.layer addAnimation:transition forKey:nil];
  [self.view addSubview:PadViewController.view];
}
如果您需要更多的澄清,请在这里发布。

我该如何实现上下交换? - Raju

16

实现这个(didload)

//........towards right Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[urView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];

//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[urView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];   

然后这样:

- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{
//Do moving
}

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{
// do moving
}

谢谢 rptwsthi。请问在“do moving”中我应该做什么?只加载一个视图控制器就可以了吗? - Akshay
抱歉晚了,是的,你可以加载、控制器或添加子视图,但我不知道如何进行动画处理。 :I - rptwsthi
@rptwsthi 这段代码完美无缺!非常感谢你! - cardmagik
@cardmagik为您服务。 :) - rptwsthi
这个可以工作,但导航非常快。有没有办法减慢它的速度或者让它像苹果一样(在设置中)工作? - software is fun
为此,您可能需要使用平移手势识别器进行工作! - rptwsthi

5
当然可以!只需将您的视图控制器设置为UIGestureRecognizerDelegate,并声明UISwipeGestureRecognizer *swipeLeftRecognizer;(还要保留和综合)。然后,在实现中,使用以下代码设置识别器:
    UIGestureRecognizer *recognizer;
    // RIGHT SWIPE
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                           action:@selector(handleSwipeFrom:)];
    [self.view addGestureRecognizer:recognizer];
    [recognizer release];
    // LEFT SWIPE
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeftRecognizer];
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
    [recognizer release];

然后使用该方法触发您想要的操作。
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        // load a different viewController
    } else {
        // load an even different viewController
    }
}

这里所做的是针对您的应用程序进行特定操作。您可以切换选项卡栏选择,通过导航控制器跳转,以模态方式呈现不同的视图,或仅执行简单的滑动过渡。


谢谢您的快速响应,PengOne。还有一个问题 - 我想让这个视图像滚动屏幕一样加载。我该怎么做? - Akshay
动画...你可以使用简单的UIViewAnimationTransition或者做一个更加花哨的自定义动画。在谷歌上搜索一些不错的例子。 - PengOne
我知道我之前在SO上看到过一篇帖子,其中提供了获取类似于navigationControllers默认的漂亮滑动转换效果的指令,但是我现在找不到它了。我相信只要你使用几个关键词搜索就能找到它。祝你好运! - PengOne

2

在Swift中添加手势

func addSwipes() {
    // Left Swipe
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipeLeft:")
    swipeLeft.direction = .Left
    self.view.addGestureRecognizer(swipeLeft)

    // Right Swipe
    let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipeRight:")
    swipeRight.direction = .Right
    self.view.addGestureRecognizer(swipeRight)
}

func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) {
}

func swipeRight(gestureRecognizer: UISwipeGestureRecognizer) {

}

1

参考 PengOne,这是你在上面的评论中提到的代码。我保存了它在我的 Mac 上,因为我认为有一天它可能会有用... :D

// Manual navigation push animation
UIImage* image = [UIImage imageNamed:@"CurrentView"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = self.view.bounds;
[self.view addSubview:imageView];
[imageView release];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.layer addAnimation:transition forKey:@"push-transition"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接