简体   繁体   中英

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. On my main view controller I have a view and I am calling the content from other 5 view controllers to that view. And I want to swipe those 5 view controllers.

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 .

With the setViewControllers(_:direction:animated:completion:) you can set an array of your view controllers and also you can customise the animation.

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

Add on all of your viewController two 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 ). 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 .
Create from each view controller to the next a Show seague and one back. Give them an Identifier you can remember. Then write something like that into each ViewController.swift and link the @IBAction with the two Swipe Gesture Recognizer of each corresponding view controller:

 @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)
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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