简体   繁体   English

从左到右过渡屏幕?

[英]Transition Screens from Left to Right?

I would like to create a segue. 我想创建一个segue。 I am using Swift. 我正在使用Swift。 I want the new View Controller to slide in from the left. 我希望新的View Controller从左侧滑入。 This means that the previous View Controller will slide out from left to right. 这意味着以前的View Controller将从左向右滑出。 I have tried the segues Push, Replace, Present Modally, and Present as Popover but none of them do the left to right transition. 我已经尝试过segue的“推”,“替换”,“模态呈现”和“呈现为弹出框”,但是它们都不执行从左到右的过渡。 They either have a right to left transition or the new screen appears from the bottom of the screen. 它们具有从右到左的过渡,或者新的屏幕从屏幕底部出现。 How can I make a left to right transition? 如何进行从左到右的过渡? Thanks. 谢谢。

You could write a custom transition that would not be too difficult but if you don't want to go that route here is a simple fix that will work. 您可以编写一个自定义过渡,这不会太困难,但是如果您不想走那条路线,这里的简单修复将起作用。 Click on the segue and unclick/disable animates on the segue. 单击设置,然后取消/禁用设置动画。 Then in prepareForSegue use this. 然后在prepareForSegue中使用它。

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let _ = segue.destination as? NextViewController{
            let trans = CATransition()
            trans.type = kCATransitionMoveIn
            trans.subtype = kCATransitionFromLeft
            trans.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            trans.duration = 0.35
            self.navigationController?.view.layer.add(trans, forKey: nil)
        }
    }

Feel free to explore the other options with CATransition types . 随意探索CATransition 类型的其他选项。 You can have some serious fun with them. 您可以和他们一起玩得很开心。 Now I am guessing you want to have the reverse for the back button press in the next view controllers. 现在,我猜您想在下一个视图控制器中按下后退按钮的操作与此相反。 You will have to replace the back button and to intercept the back press and pop without animation so you can add your own animation. 您将不得不替换“后退”按钮,并拦截没有动画的后退按下并弹出,因此您可以添加自己的动画。 Here is what that would look like. 这是什么样子。

 override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(backTapped))
}

@objc func backTapped() {
    let trans = CATransition()
    trans.type = kCATransitionMoveIn
    trans.subtype = kCATransitionFromRight
    trans.duration = 0.3
    self.navigationController?.view.layer.add(trans, forKey: nil)
    navigationController?.popViewController(animated: false)
}

Again the other option would be use UIViewControllerAnimatedTransitioning but depending on your skill level and comfort with animations I find the above an easy implementation. 同样,另一个选择是使用UIViewControllerAnimatedTransitioning,但是根据您的技术水平和对动画的舒适度,我发现上述方法很容易实现。

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

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