IOS容器视图在它们之间滑动
Posted
技术标签:
【中文标题】IOS容器视图在它们之间滑动【英文标题】:IOS container view swipe between them 【发布时间】:2019-08-12 10:57:27 【问题描述】:我设计了一个应用程序,该应用程序具有不会移动的纯色背景,然后是一系列可在此之上滑动的页面。当您滑动时,我希望背景保持不变,并且当您滑动替换最后一个面板时,新面板会从左向右移动。
为此,我假设我会为每个要显示的视图使用容器视图,并像这样链接它们。
https://spin.atomicobject.com/2015/09/02/switch-container-views/
我缺少的一点是滑动,如果这是做这种事情的正确方法。
任何指向正确方向的指针都会很棒
【问题讨论】:
使用UIPageViewController
-> appcoda.com/uipageviewcontroller-storyboard-tutorial
有很多 pod 可以实现这样的功能,请参考下面的 Pod 它可能对你有帮助 github.com/hirohisa/PageController
【参考方案1】:
您可以使用 UIPageViewController,在其上添加静态背景。
这是一个例子:
class YourCustomPageViewController: UIPageViewController
// for example purpose i will use only two view controllers, you can use more
var firstVC: CustomViewController?
var secondVC: CustomViewController?
//MARK: Private Properties
fileprivate var pages: [UIViewController] = []
//MARK: Object Life Cycle
override func viewDidLoad()
super.viewDidLoad()
self.view.backgroundColor = .white
dataSource = self
delegate = self
//This is your static background, which will be added on page view controller and will not be scrollable
let bgImageView = UIImageView(image: UIImage(named: "bg"))
bgImageView.translatesAutoresizingMaskIntoConstraints = false
self.view.insertSubview(bgImageView, at: 0)
NSLayoutConstraint.activate([
bgImageView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0),
bgImageView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0),
bgImageView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0),
bgImageView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
])
firstVC = (self.storyboard?.instantiateViewController(withIdentifier: "FirstVCIdentifier")) as? CustomViewController
secondVC = (self.storyboard?.instantiateViewController(withIdentifier: "SecondVCIdentifier")) as? CustomViewController
pages.append(firstVC!)
pages.append(secondVC!)
setViewControllers([firstVC!], direction: .forward, animated: true, completion: nil)
//MARK: UIPageViewControllerDataSource & UIPageViewControllerDelegate
extension YourCustomPageViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let index = pages.firstIndex(of: viewController) else return nil
let previousIndex = index - 1
return previousIndex >= 0 ? pages[previousIndex] : nil
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let index = pages.firstIndex(of: viewController) else return nil
let nextIndex = index + 1
return nextIndex < pages.count ? pages[nextIndex] : nil
之后,您可以使用自定义 ui 构建您的 CustomViewController...它可以在 PageViewController 上滚动。
不要忘记从 pageCurl 到滚动的过渡样式。
【讨论】:
以上是关于IOS容器视图在它们之间滑动的主要内容,如果未能解决你的问题,请参考以下文章