xcode UIScroll 入门理解

基于对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]
    }
}


This entry was posted in Xcode and tagged , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/xcode-uiscroll-simple/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

  

  

About Me

静水流深,水滴石穿