我想在我的应用中添加滑动操作。基本上我有5个视图控制器和我的主视图控制器。在我的主视图控制器上,我有一个视图,我正在调用其他5个视图控制器中的内容到该视图。我想刷这5个视图控制器。
我的代码:
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
}
}
}
答案 0 :(得分:2)
你必须去UIPageViewController。
使用setViewControllers(_:direction:animated:completion:),您可以设置视图控制器的数组,也可以自定义动画。
答案 1 :(得分:0)
我不知道我是否理解了您的问题,但您可以(从Interface Builder)添加UIGestureRecognizer(用于滑动操作)到View以及该手势的选择器中,您可以显示View&# 39;我想表现出来。
答案 2 :(得分:0)
在你的所有viewController上添加两个Swipe Gesture Recognizer
。请务必将它们拖放到层次结构中的View Controller
,因为否则它可能不起作用(对于最左侧和最右侧的视图控制器,您只需添加一个swipe gesture recognizer
)。在对Swipe Gesture Recognizer
每个视图控制器之一进行更改之后,属性检查器中的Swipe
选项从标准Left
更改为Right
。
从每个视图控制器创建到下一个Show
seague和一个后面。给他们一个你能记住的Identifier
。然后在每个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)
}
}