1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var pageControl: UIPageControl!
var scrollViewHeader: UIScrollView!
var scrollViewMain: UIScrollView!
let pageSize = 10
override func viewDidLoad() {
let width = self.view.frame.maxX, height = self.view.frame.maxY
scrollViewHeader = UIScrollView(frame: self.view.frame)
scrollViewHeader.showsHorizontalScrollIndicator = false
scrollViewHeader.showsVerticalScrollIndicator = false
scrollViewHeader.pagingEnabled = true
scrollViewHeader.delegate = self
scrollViewHeader.contentSize = CGSizeMake(CGFloat(pageSize) * width, 0)
self.view.addSubview(scrollViewHeader)
scrollViewMain = UIScrollView(frame: self.view.frame)
scrollViewMain.showsHorizontalScrollIndicator = false
scrollViewMain.showsVerticalScrollIndicator = false
scrollViewMain.pagingEnabled = true
scrollViewMain.delegate = self
scrollViewMain.contentSize = CGSizeMake(CGFloat(pageSize) * width, 0)
self.view.addSubview(scrollViewMain)
for var i = 0; i < pageSize; i++ {
let myLabel:UILabel = UILabel(frame: CGRectMake(CGFloat(i) * width + width/2 - 40, height/2 - 40, 80, 80))
myLabel.backgroundColor = UIColor.blackColor()
myLabel.textColor = UIColor.whiteColor()
myLabel.textAlignment = NSTextAlignment.Center
myLabel.layer.masksToBounds = true
myLabel.text = "Page\(i)"
myLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
myLabel.layer.cornerRadius = 40.0
scrollViewMain.addSubview(myLabel)
}
for var i = 0; i < pageSize; i++ {
let myLabel:UILabel = UILabel(frame: CGRectMake(CGFloat(i) * width/4 + width/2 - 40, 50, 80, 60))
myLabel.backgroundColor = UIColor.redColor()
myLabel.textColor = UIColor.whiteColor()
myLabel.textAlignment = NSTextAlignment.Center
myLabel.layer.masksToBounds = true
myLabel.text = "Page\(i)"
myLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
myLabel.layer.cornerRadius = 30.0
scrollViewHeader.addSubview(myLabel)
}
pageControl = UIPageControl(frame: CGRectMake(0, self.view.frame.maxY - 50, width, 50))
pageControl.backgroundColor = UIColor.lightGrayColor()
pageControl.numberOfPages = pageSize
pageControl.currentPage = 0
pageControl.userInteractionEnabled = false
self.view.addSubview(pageControl)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == scrollViewMain {
scrollViewHeader.contentOffset.x = scrollViewMain.contentOffset.x/4
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if fmod(scrollViewMain.contentOffset.x, scrollViewMain.frame.maxX) == 0 {
pageControl.currentPage = Int(scrollViewMain.contentOffset.x / scrollViewMain.frame.maxX)
}
}
}
|