cmi*_*mii 5 ios uipageviewcontroller swift
我使用了这个惊人的教程如何在Swift中使用UIPageViewController来理解和实现我的应用程序中的UIPageViewController.
我已成功完成集成,但我现在需要修改一点行为.
我不想一次只查看一个彩色视图,而是查看前一个视图的25%和下一个视图的25%.
我想我必须使用这个方法传递3个viewcontrollers:
func setViewControllers(_ viewControllers: [UIViewController]?,
direction: UIPageViewControllerNavigationDirection,
animated: Bool,
completion: ((Bool) -> Void)? = nil)
Run Code Online (Sandbox Code Playgroud)
......但我不知道该怎么做
当您使用滚动视图创建 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.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1961 次 |
| 最近记录: |