ios - 如何使用UIPageViewController同时显示部分上一个和下一个 View

标签 ios swift uipageviewcontroller

我使用了这个很棒的教程 How to Use UIPageViewController in Swift了解并在我的应用中实现 UIPageViewController。

我已经成功完成了集成,但我现在需要稍微修改一下行为。

我不想一次只查看一个彩色 View ,而是想查看前一个 View 的 25% 和下一个 View 的 25%。

enter image description here

我想我必须使用这个方法传递 3 个 View Controller :

func setViewControllers(_ viewControllers: [UIViewController]?, 
          direction: UIPageViewControllerNavigationDirection, 
           animated: Bool, 
         completion: ((Bool) -> Void)? = nil)

...但是我不知道该怎么做

最佳答案

当您使用 ScrollView 创建 UIPageviewcontroller 时,这很容易实现。是的,您可以使用 UIScrollView 来显示它,就像 pageviewcontroller 一样。现在显示矩形(在您的情况下为当前屏幕 + 第二个屏幕的 25%)在您手中。

下面是相关代码。

    import UIKit

class ViewController: UIViewController,UIScrollViewDelegate {

    let scrollView = UIScrollView(frame: CGRectMake(0, 0, 320, 300))
    var colors:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor()]
    var frame: CGRect = CGRectMake(0, 0, 0, 0)
    var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 50))


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         configurePageControl()

        scrollView.delegate = self
        self.view.addSubview(scrollView)
        for index in 0..<4 {

            frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
            frame.size = self.scrollView.frame.size
            self.scrollView.pagingEnabled = true

            var subView = UIView(frame: frame)
            subView.backgroundColor = colors[index]
            self.scrollView .addSubview(subView)
        }

        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, self.scrollView.frame.size.height)
        pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)

           }

    func configurePageControl() {
        // The total number of pages that are available is based on how many available colors we have.
        self.pageControl.numberOfPages = colors.count
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.redColor()
        self.pageControl.pageIndicatorTintColor = UIColor.blackColor()
        self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor()     
        self.view.addSubview(pageControl)

         }

    // MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
    func changePage(sender: AnyObject) -> () {
        let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
        scrollView.setContentOffset(CGPointMake(x, 0), animated: true)
    }

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = Int(pageNumber)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

关于ios - 如何使用UIPageViewController同时显示部分上一个和下一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41284846/

相关文章:

ios - 如何以正确的方式回到初始的 ViewController?

ios - 表格 View 滚动平滑与 url 图像

iphone - ios如何在小视野范围内打开相机

macos - CVDisplayLink 与 Swift

ios - UICollectionReusableView 标题不调整旋转大小 - 约束?

ios - SwiftUI - 使用新页面更新页面 View Controller

iphone - pageviewcontroller 中的 PageCurlEffect 与工具栏重叠

ios - 基于页面的应用程序和手势识别器

ios - 如何在 Storyboard 中处理添加到 ViewController 主视图之外的 View ?

swift - 使用带有自定义参数的协议(protocol)函数