Swift实现UIScrollView展示多个UIViewController
来源:互联网 发布:淘宝哪家的男牛仔裤好 编辑:程序博客网 时间:2024/06/01 09:01
效果图:
----------FirstPageViewController-----------
override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brownColor() }
----------SecondPageViewController-----------
override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.purpleColor() }
----------ThirdPageViewController-----------
override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.blueColor() }
---------------PageViewController--------------------
//// PageControlViewController.swift//import UIKitclass PageControlViewController: UIViewController, UIScrollViewDelegate{ var pageControl: UIPageControl? var scrollView: UIScrollView? var isPageControlUsed = false var screenFrame :CGRect? override func viewDidLoad() { super.viewDidLoad() self.title = "UIPageControl" self.view.backgroundColor = UIColor.whiteColor() screenFrame = UIScreen.mainScreen().bounds let screenWidth = screenFrame!.width let screenHeight = screenFrame!.height var view = UIView(frame: CGRectMake(0, 0, screenWidth, 60)) view.backgroundColor = UIColor.whiteColor() self.view.addSubview(view) scrollView = UIScrollView(frame: CGRectMake(0, 60, screenWidth, screenHeight - 60)) pageControl = UIPageControl(frame: CGRectMake(0, screenHeight - 40, screenWidth, 40)) scrollView!.pagingEnabled = true scrollView!.contentSize = CGSizeMake(screenWidth * 3, screenHeight - 60) let firstViewController = FirstPageViewController() firstViewController.view.frame = CGRectMake(0, 0, screenWidth, screenHeight - 60) let secondViewController = SecondPageViewController() secondViewController.view.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight - 60) let thirdViewController = ThirdPageViewController() thirdViewController.view.frame = CGRectMake(screenWidth * 2, 0, screenWidth, screenHeight - 60) scrollView!.addSubview(firstViewController.view) scrollView!.addSubview(secondViewController.view) scrollView!.addSubview(thirdViewController.view) scrollView!.delegate = self self.view.addSubview(scrollView!) pageControl!.numberOfPages = 3 pageControl!.currentPage = 0 pageControl!.addTarget(self, action: "pageDidChanged:", forControlEvents: UIControlEvents.ValueChanged) self.view.addSubview(pageControl!) installCodeBarBtn() } func pageDidChanged(obj : UIPageControl) -> Void { var currentPage = obj.currentPage var frame = scrollView!.frame frame.origin.x = (CGFloat)(currentPage) * frame.size.width scrollView!.scrollRectToVisible(frame, animated: true) isPageControlUsed = true } func scrollViewDidScroll(scrollView: UIScrollView) { if(!isPageControlUsed) { let pageWidth = scrollView.frame.size.width let page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1 pageControl!.currentPage = (Int)(page) } } func scrollViewDidEndDecelerating(scrollView: UIScrollView) { isPageControlUsed = false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func installCodeBarBtn() -> Void { var rightBar = UIBarButtonItem(title: "Code", style: UIBarButtonItemStyle.Done, target: self, action: "toCodeViewPad") self.navigationItem.rightBarButtonItem = rightBar } func toCodeViewPad() -> Void { let codeViewController = PageCodeViewController() self.navigationController!.pushViewController(codeViewController, animated: true) }}
1 0
- Swift实现UIScrollView展示多个UIViewController
- Swift开发--多个UIViewController之间传值
- 一个UIViewController控制器中,实现多个UITableView的方法
- Swift - 利用UIScrollView 实现轮播图
- swift UIScrollView
- UIScrollView利用分页展示多张图片
- [Swift 开发] Swift UIScrollView
- 用swift实现tableview的展示
- swift UIViewController生命周期
- Swift 利用UIScrollView和两个UIImageView实现图片轮滚
- 可自定义分页宽度的UIScrollView(Swift实现)
- 多个uiviewController之间传值
- 多个UIViewController中复用NIB文件
- Swift UIScrollView简单介绍
- swift UIScrollView简单介绍
- Swift UIScrollView(滑动视图)
- Swift-UIScrollView使用详解
- Swift-UIScrollView 应用
- MagicalRecord入门教程
- Spark源码阅读环境搭建[Windows版]
- android-activity启动模式
- 权限框架Spring secutity
- CodeVS1907 方格取数3
- Swift实现UIScrollView展示多个UIViewController
- Mybatis
- 代理模式(二):代理模式应用实例(收费商务信息查询系统)
- OA系统
- 1037. Magic Coupon (25)
- 1020. 月饼 (25)
- 关于初学centos网络配置问题的解决方法
- Docker为什么刚运行就退出了?
- SSLSOcket在Android6.0中出错原因