简体   繁体   English

如何在视图控制器之间滑动?

[英]How can I swipe between view controllers?

I want to add a swipe action to my app. 我想向我的应用添加滑动操作。 Basically I have 5 view controllers and I main view controller. 基本上我有5个视图控制器和主视图控制器。 On my main view controller I have a view and I am calling the content from other 5 view controllers to that view. 在我的主视图控制器上,我有一个视图,并且正在从其他5个视图控制器调用该视图的内容。 And I want to swipe those 5 view controllers. 我想滑动这5个视图控制器。

My code: 我的代码:

import UIKit

class TabViewController: UIViewController {


    @IBOutlet var contentView: UIView!
    @IBOutlet var buttons: [UIButton]!
    @IBOutlet var backgroundView: UIImageView!


    var movingView = UIView()

    var rifleViewController: UIViewController!
    var pistolViewController: UIViewController!
    var shotgunViewController: UIViewController!
    var smgsViewController: UIViewController!
    var sniperViewController: UIViewController!

    var viewControllers: [UIViewController]!
    var selectedIndex: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        rifleViewController = storyboard.instantiateViewController(withIdentifier: "rifles")
        sniperViewController = storyboard.instantiateViewController(withIdentifier: "snipers")
        smgsViewController = storyboard.instantiateViewController(withIdentifier: "smgss")
        shotgunViewController = storyboard.instantiateViewController(withIdentifier: "shotguns")
        pistolViewController = storyboard.instantiateViewController(withIdentifier: "pistols")
        viewControllers = [rifleViewController,
                           pistolViewController,
                           shotgunViewController,
                           smgsViewController,
                           sniperViewController]

        buttons[selectedIndex].isSelected = true
        didPressTab(buttons[selectedIndex])

        let screenWidth = UIScreen.main.bounds.width
        movingView = UIView(frame: CGRect(x: 0, y: 80, width: screenWidth / 5, height: 5))
        movingView.backgroundColor = UIColor.white
        backgroundView.addSubview(movingView)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func didPressTab(_ sender: UIButton) {

        let previousIndex = selectedIndex

        selectedIndex = sender.tag

        buttons[previousIndex].isSelected = false
        let previousVC = viewControllers[previousIndex]

        previousVC.willMove(toParentViewController: nil)
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParentViewController()

        sender.isSelected = true

        let vc = viewControllers[selectedIndex]

        addChildViewController(vc)

        vc.view.frame = contentView.bounds
        contentView.addSubview(vc.view)
        vc.didMove(toParentViewController: self)

        let newx = sender.frame.origin.x

        UIView.animate(withDuration: 0.2) {
            self.movingView.frame.origin.x = newx
        }


        }

}

You must go for UIPageViewController . 您必须使用UIPageViewController

With the setViewControllers(_:direction:animated:completion:) you can set an array of your view controllers and also you can customise the animation. 使用setViewControllers(_:direction:animated:completion :),您可以设置视图控制器的数组,还可以自定义动画。

我不知道我是否理解您的问题,但是您可以(从Interface Builder中)添加一个UIGestureRecognizer(用于滑动操作)到“视图”中,并在该手势的选择器中提供您想要显示的视图。

Add on all of your viewController two Swipe Gesture Recognizer . 在所有viewController上添加两个Swipe Gesture Recognizer Take care that you really drag and drop them onto the View Controller in the hierarchy because else it might not work (For the most left and most right view controller you just have to add one swipe gesture recognizer ). 请注意,您确实要将它们拖放到层次结构中的View Controller上,因为否则可能不起作用(对于最左侧和最右侧的View Controller,您只需添加一个swipe gesture recognizer )。 After that change for one of the the Swipe Gesture Recognizer per view controller the Swipe option in the attribute inspector from the standard Left to Right . 在针对每个视图控制器之一的“ Swipe Gesture Recognizer器”进行更改之后,属性检查器中的“ Swipe选项从标准的“ Left Right Left更改。
Create from each view controller to the next a Show seague and one back. 从每个视图控制器创建到下一个Show seague,然后再返回。 Give them an Identifier you can remember. 给他们一个您可以记住的Identifier Then write something like that into each ViewController.swift and link the @IBAction with the two Swipe Gesture Recognizer of each corresponding view controller: 然后在每个ViewController.swift编写类似的内容,并将@IBAction与每个相应视图控制器的两个Swipe Gesture Recognizer链接:

 @IBAction func didSwipe(_ sender: UISwipeGestureRecognizer) {
      if sender.direction == UISwipeGestureRecognizerDirection.left {
          performSegue(withIdentifier: "identifierOfSegue", sender: nil)
      }

      if sender.direction == UISwipeGestureRecognizerDirection.right {
          performSegue(withIdentifier: "identifierOfOtheSegue", sender: nil)
      }
 }

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

相关问题 在视图控制器之间滑动手势 - Swipe gesture between view controllers 在视图控制器之间平移(不滑动) - Pan (not swipe) between view controllers 用户可以滑动或单击按钮以在视图控制器之间导航 - The user can either swipe or click the buttons to Navigate between view Controllers 在故事板中的视图控制器之间滑动手势 - Swipe gesture between view controllers in storyboards 设置UIScrollView以在3个视图控制器之间滑动 - Setting up UIScrollView to swipe between 3 view controllers 使用UIPageViewController在多个视图控制器之间滑动 - Swipe between multiple view controllers using UIPageViewController 如何在Swift上的两个子视图控制器之间切换? - How can I switch between two child View Controllers on Swift? 如何为两个模态视图控制器之间的过渡设置动画? - How can I animate the transition between two modal view controllers? 如何在不在同一个故事板上的视图控制器之间以编程方式切换? - How can I switch programmatically between view controllers that are not on the same storyboard? 如何使用动画在控制器之间滑动? - How to make swipe between Controllers with animation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM