[英]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.