简体   繁体   English

在视图控制器之间平移(不滑动)

[英]Pan (not swipe) between view controllers

I am creating an app that has a user interface similar to Tinder. 我正在创建一个具有类似于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. 最终结果看起来和感觉非常不自然。

This is how I'd like my user interface to behave (ignore the Gecko): 这就是我希望用户界面表现出来的方式(忽略Gecko):

在此处输入图片说明

This is how it currently behaves with a swipe gesture recognizer: 这是当前滑动手势识别器的行为: 在此处输入图片说明

Notice how with Tinder, you can pan to a new view controller without having completely committed the segue. 请注意,使用Tinder可以平移到新的视图控制器而无需完全提交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)
    }
}

I am also utilizing a custom UIViewControllerAnimatedTransitioning to make the pushed view controllers push the presenting view controller out from the left or right. 我还利用自定义UIViewControllerAnimatedTransitioning来使推送的视图控制器从左侧或右侧推送呈现的视图控制器。 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的导航栏。

I'm not sure UINavigationController is the correct container controller to provide what you need. 我不确定UINavigationController是提供您所需内容的正确容器控制器。 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. 您可能会发现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. 要走自定义路线(使用UINavigationController或更灵活/可自定义的UIViewController自定义表示法),您将在正确的方向上使用UIViewControllerAnimatedTransitioning和其他(众多)关联协议。 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. 如果您很着急,该路线将花费您很长的时间来实现,因此暂时可能需要执行一个简单的UIPageViewController实现。

github上的这个项目可能会帮助您实现要求- 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM