gpt4 book ai didi

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

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

我使用了这个很棒的教程 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/

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