import
UIKit
class
ViewController
:
UIViewController
,
UIScrollViewDelegate
{
@IBOutlet
var
pageControl:
UIPageControl
!
@IBOutlet
var
scrollView:
UIScrollView
!
var
courses = [
[
"name"
:
"Swift"
,
"pic"
:
"swift.png"
],
[
"name"
:
"ObjectC"
,
"pic"
:
"oc.jpg"
],
[
"name"
:
"Java"
,
"pic"
:
"java.png"
]
]
override
func
viewDidLoad() {
super
.viewDidLoad()
scrollView.contentSize =
CGSizeMake
(
CGFloat
(
CGRectGetWidth
(
self
.view.bounds)) *
CGFloat
(
self
.courses.count),
CGRectGetHeight
(
self
.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
enumerate
(courses) {
var
page =
UIView
()
var
imageView=
UIImageView
(image:
UIImage
(named:course[
"pic"
]!))
page.addSubview(imageView);
page.backgroundColor =
UIColor
.greenColor()
let
lbl =
UILabel
(frame:
CGRect
(x: 0, y: 20, width: 100, height: 20))
lbl.text = course[
"name"
]
page.addSubview(lbl)
page.frame =
CGRect
(x:
CGFloat
(seq) * size.width, y: 0,
width: size.width, height: size.height)
scrollView.addSubview(page)
}
pageControl.backgroundColor =
UIColor
.clearColor()
pageControl.numberOfPages = courses.count
pageControl.currentPage = 0
pageControl.addTarget(
self
, action:
"pageChanged:"
, forControlEvents:
UIControlEvents
.
ValueChanged
)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
func
scrollViewDidEndDecelerating(scrollView:
UIScrollView
!) {
let
page =
Int
(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = page
}
func
pageChanged(sender:
UIPageControl
) {
var
frame = scrollView.frame
frame.origin.x = frame.size.width *
CGFloat
(sender.currentPage)
frame.origin.y = 0
scrollView.scrollRectToVisible(frame, animated:
true
)
}
}
请发表评论