在视图控制器之间平移(不滑动) [英] Pan (not swipe) between view controllers

查看:20
本文介绍了在视图控制器之间平移(不滑动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户界面类似于 Tinder 的应用.

I am creating an app that has a user interface similar to Tinder.

这里,我的意思是可以在三个主要视图控制器之间平移".用户可以简单地拖动以移动到三个视图控制器中的任何一个.

By this, I mean that there are three primary view controllers that can be "panned" between. The user can simply drag to move to any of the three view controllers.

我在我的应用中实现了类似的功能,但有一个重要的警告:它使用滑动手势识别器,而不是平移手势识别器.

I have implemented something similar in my app with one important caveat: it utilizes swipe gesture recognizers and not pan gesture recognizers.

最终结果看起来和感觉非常不自然.

The end-result looks and feels very unnatural.

这就是我希望我的用户界面的行为方式(忽略 Gecko):

This is how I'd like my user interface to behave (ignore the Gecko):

这是它当前使用滑动手势识别器的行为方式:

This is how it currently behaves with a swipe gesture recognizer:

注意如何使用 Tinder,您可以在不完全提交 segue 的情况下平移到新的视图控制器.并且,如果平底锅没有将当前视图控制器移动到超过最小阈值,它会简单地弹回原位.这是我正在寻找的行为.

Notice how with Tinder, you can pan to a new view controller without having completely committed the segue. And, if the pan hasn't displaced the current view controller beyond a minimum threshold, it will simply snap back into place. This is the behavior I am looking for.

这是我目前用来模拟滑动手势识别器的一些代码:

Here is some code that I am currently using to mimic with a swipe gesture recognizer:

//The following logic is applied in a similar way to view controllers 1 and 3 as well.

class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addSwipeGestureRecognizers()
    }

    func addSwipeGestureRecognizers() {
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeLeft))
        swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
        self.view.addGestureRecognizer(swipeLeft)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeRight))
        swipeRight.direction = UISwipeGestureRecognizer.Direction.right
        self.view.addGestureRecognizer(swipeRight)
    }

    @objc func handleSwipeLeft() {
        let storyboard = UIStoryboard(name: "Main", bundle: .main)
        let viewController1 = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
        let transition:CATransition = CATransition()
        transition.duration = 0.25
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.push
        transition.subtype = CATransitionSubtype.fromLeft
        self.navigationController!.view.layer.add(transition, forKey: kCATransition)
        navigationController!.pushViewController(viewController1, animated: false)
    }

    @objc func handleSwipeRight() {
        let storyboard = UIStoryboard(name: "Main", bundle: .main)
        let viewController3 = storyboard.instantiateViewController(withIdentifier: "ViewController3") as! ViewController3
        let transition:CATransition = CATransition()
        transition.duration = 0.25
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.push
        transition.subtype = CATransitionSubtype.fromRight
        self.navigationController!.view.layer.add(transition, forKey: kCATransition)
        navigationController!.pushViewController(viewController3, animated: false)
    }
}

我还利用自定义的 UIViewControllerAnimatedTransitioning 使推送的视图控制器从左侧或右侧推出呈现视图控制器.这是与呈现视图控制器顶部的默认堆叠相反的.我也隐藏了 UINavigationController 的导航栏.

I am also utilizing a custom UIViewControllerAnimatedTransitioning to make the pushed view controllers push the presenting view controller out from the left or right. This is done as opposed to the default stacking on top of the presenting view controller. I also have my UINavigationController's navigation bar hidden.

推荐答案

我不确定 UINavigationController 是提供您需要的正确容器控制器.你可能会发现 UIPageViewController 是一个更好的选择,我认为它会提供你正在寻找的开箱即用的自然滑动手势,尽管它是一个相当不透明的类,有一些自己的怪癖.

I'm not sure UINavigationController is the correct container controller to provide what you need. You might find that UIPageViewController is a better choice, I think it will provide the natural swipe gesture you are looking for out of the box, although it's quite an opaque class with some quirks of its own.

要走自定义路线(使用 UINavigationController 或更灵活/可定制的 UIViewController 自定义演示文稿),您使用 UIViewControllerAnimatedTransitioning 和其他(众多)相关协议走在正确的轨道上.如果您赶时间,那么该路由将花费您很长时间来实现,因此目前可能需要一个简单的 UIPageViewController 实现.

To go the custom route (either with UINavigationController or the more flexible/customisable UIViewController custom presentation) you are on the right track with UIViewControllerAnimatedTransitioning and the other (numerous) associated protocols. In case you are in a hurry, that route will take you quite a long time to implement, so maybe a simple UIPageViewController implementation will have to do for the moment.

这篇关于在视图控制器之间平移(不滑动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆