基于对UIScroll的简单的理解。
实现横向翻页 3 页
let numOfPages = 3
let pageWidth = 320
let pageHeight = 360
override func viewDidLoad() {
super.viewDidLoad()
var scrollView = UIScrollView()
scrollView.frame = self.view.bounds
//为了让内容横向滚动,设置横向内容宽度为3个页面的宽度总和
scrollView.contentSize=CGSizeMake(CGFloat(pageWidth*numOfPages), CGFloat(pageHeight))
scrollView.pagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
//添加子页面
for i in 0...numOfPages{
var myViewController = MyViewController(number:(i+1))
myViewController.view.frame = CGRectMake(CGFloat(pageWidth*i),
CGFloat(0), CGFloat(pageWidth), CGFloat(pageHeight))
scrollView.addSubview(myViewController.view)
}
self.view.addSubview(scrollView)
}
//写在同一个类里就好了
class MyViewController: UIViewController{
var number:Int!
let colorMap=[
1:UIColor.blackColor(),
2:UIColor.orangeColor(),
3:UIColor.blueColor()
]
init(number initNumber:Int){
self.number = initNumber
super.init(nibName:nil, bundle:nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad(){
var numberLabel = UILabel(frame:CGRectMake(0,0,100,100))
numberLabel.center = self.view.center
numberLabel.text = "第\(number)页"
numberLabel.textColor = UIColor.whiteColor()
self.view.addSubview(numberLabel)
self.view.backgroundColor = colorMap[number]
}
}