How to use UIPageViewController to display part of previous and next views

I used this awesome tutorial How to use UIPageViewController in Swift to understand and implement UIPageViewController in my application.

I successfully completed the integration, but I need to change the behavior a bit.

Instead of viewing only one color view at a time, I would like to view 25% of the previous view and 25% of the next.

enter image description here

I think I need to use this method, passing 3 view managers:

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

... but I do not know how to do it

+5
source share
1 answer

This is easily achievable when you create a UIPageviewcontroller with a scroll list. Yes, you can use UIScrollView to show it as a pageviewcontroller. Now you have a display rectangle in your hands (in your current screen + 25% of the second screen).

Below is the code for this.

  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. } } 

Here is a good tutorial to help you with this. PageView custom controller with scrolling>

0
source

Source: https://habr.com/ru/post/1261721/


All Articles