Last active
August 29, 2015 14:14
-
-
Save nishiyamaosamu/baac5d9f4b05e52c470c to your computer and use it in GitHub Desktop.
[Swift]たった40行のUIScrollViewを使ったシンプルなチュートリアル画面サンプル ref: http://qiita.com/osamu1203/items/a1a361f9ff00e93258e2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController, UIScrollViewDelegate { | |
var scrollView: UIScrollView! | |
var pageControll: UIPageControl! | |
let pageNum = 4 | |
let pageColors:[Int:UIColor] = [1:UIColor.redColor(),2:UIColor.yellowColor(),3:UIColor.blueColor(),4:UIColor.greenColor()] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.scrollView = UIScrollView(frame: self.view.bounds) | |
self.scrollView.contentSize = CGSizeMake(self.view.bounds.width * CGFloat(pageNum), self.view.bounds.height) | |
self.scrollView.pagingEnabled = true | |
self.scrollView.showsHorizontalScrollIndicator = false | |
self.scrollView.delegate = self; | |
self.view.addSubview(self.scrollView) | |
self.pageControll = UIPageControl(frame: CGRectMake(0, self.view.bounds.height-50, self.view.bounds.width, 50)) | |
self.pageControll.numberOfPages = pageNum | |
self.pageControll.currentPage = 0 | |
self.view.addSubview(self.pageControll) | |
for p in 1...pageNum { | |
var v = UIView(frame: CGRectMake(self.view.bounds.width * CGFloat(p-1), 0, self.view.bounds.width, self.view.bounds.height)) | |
v.backgroundColor = self.pageColors[p] | |
self.scrollView.addSubview(v) | |
} | |
} | |
func scrollViewDidScroll(scrollView: UIScrollView) { | |
var pageProgress = Double(scrollView.contentOffset.x / scrollView.bounds.width) | |
self.pageControll.currentPage = Int(round(pageProgress)) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment