import
UIKit
class
ViewController
:
UIViewController
,
UIScrollViewDelegate
{
//界面设计元素引用
@IBOutlet
var
pageControl:
UIPageControl
!
scrollView:
UIScrollView
!
courses = [
[
"name"
:
"Swift"
"pic"
"swift.png"
],
"ObjectC"
"oc.jpg"
"Java"
"java.png"
]
]
override
func
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
//设置scrollView的内容总尺寸
scrollView.contentSize =
CGSizeMake
(
CGFloat
(
CGRectGetWidth
(
self
.view.bounds)) *
.courses.count),
CGRectGetHeight
.view.bounds)
)
scrollView.showsHorizontalScrollIndicator =
false
scrollView.showsVerticalScrollIndicator =
false
scrollView.scrollsToTop =
false
//协议代理,在本类中处理滚动事件
scrollView.delegate =
self
//滚动时只能停留到某一页
scrollView.pagingEnabled =
true
let
size = scrollView.bounds.size
for
(seq,course)
in
courses.enumerate() {
page =
UIView
()
imageView=
UIImageView
(image:
UIImage
(named:course[
]!))
page.addSubview(imageView);
page.backgroundColor =
UIColor
.greenColor()
lbl =
UILabel
(frame:
CGRect
(x: 0,y: 20,width: 100,height: 20))
lbl.text = course[
]
page.addSubview(lbl)
page.frame =
(x:
(seq) * size.width,y: 0,
width: size.width,height: size.height)
scrollView.addSubview(page)
}
//页控件属性
pageControl.backgroundColor =
.clearColor()
pageControl.numberOfPages = courses.count
pageControl.currentPage = 0
//设置页控件点击事件
pageControl.addTarget(
"pageChanged:"
UIControlEvents
.
ValueChanged
)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
scrollViewDidEndDecelerating(scrollView:
!) {
page =
Int
(scrollView.contentOffset.x / scrollView.frame.size.width)
//设置pageController的当前页
pageControl.currentPage = page
}