gpt4 book ai didi

ios - 使用 Swift 3 在 iOS 中替代 Android ViewPager

转载 作者:搜寻专家 更新时间:2023-10-31 23:10:19 25 4
gpt4 key购买 nike

我正在使用 Swift 3 为 iPhone 开发一个应用程序,我对 ViewController 中页面之间的导航有一些疑问。

在我的 android 应用程序中,我有一个使用 ViewPager com 两个片段的事件。每个片段都有不同的实现并与主要事件交互。

像这样:

enter image description here

结果是:

enter image description here

我的问题是如何快速完成?

最佳答案

在ios中没有直接可用的控件,无论如何,如果你想实现与android view pager相同的功能和效果,请使用下面的控件

MainViewController{
//Design your main controller
// Place Container view in main controller (You can give custom height for container view)

}

create new view controller which extends UIPageViewController
MyPagerViewController : UIPageViewController{
// this will be directly connected to container view of Main view controller
}

reference Image

MyPagerViewController :  UIPageViewController {

var selectedBlock:Int = 1

required init?(coder aDecoder: NSCoder) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

}

override func viewDidLoad() {
super.viewDidLoad()

dataSource = self;
delegate = self;

// Setting First View
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
}


// List of UIView Controllers
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController(name: “Green”),
self.newColoredViewController(name: “Yellow”)]
}()

private func newColoredViewController(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "\(name)ViewController")
}
}

extension MyPagerViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {

func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}

let previousIndex = viewControllerIndex - 1

//selectedBlock = viewControllerIndex+1

// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return nil//orderedViewControllers.last
}

guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {




guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}

let nextIndex = viewControllerIndex + 1

//selectedBlock = viewControllerIndex+1

// print("**********" + String(selectedBlock));

let orderedViewControllersCount = orderedViewControllers.count

// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return nil//orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]enter code here
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
if(completed){
if pageViewController.viewControllers![0] is GreenViewController {
selectedBlock = 1 // this is global variable
}else{
selectedBlock = 2
}
}
}
}

关于ios - 使用 Swift 3 在 iOS 中替代 Android ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com