要实现该功能,需要如下操作:
1,将UIScrollView的pagingEnabled属性设置成true
3,最好将showsHorizontalScrollIndicator和showsVerticalScrollIndicator设置成false隐藏横向和纵向滚动条。
4,如果scrollsToTop不需要也设置成false。
--- 主页面 ---
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
|
class
ViewController
:
UIViewController
{
let
numOfPages = 3
pageWidth = 320
pageHeight = 360
override
func
viewDidLoad(){
super
.viewDidLoad()
//scrollView的初始化
var
scrollView =
UIScrollView
()
scrollView.frame =
self
.view.bounds
scrollView.contenSize=
CGSizeMake
(
CGFloat
(pageWidth*numOfPages),
(pageHeight))
scrollView.pagingEnabled =
true
scrollView.showsHorizontalScrollIndicator =
false
scrollView.showsVerticalScrollIndicator =
false
scrollView.scrollsToTop =
false
for
i
in
0..numOfPages{
myViewController =
MyViewController
(number:(i+1))
myViewController.view.frame =
CGRectMake
(pageWidth*i),
(0),monospace!important; min-height:inherit!important">(pageWidth),monospace!important; min-height:inherit!important">(pageHeight))
scrollView.addSubview(myViewController.view)
}
.view.addSubview(scrollView)
}
}
|
--- 子页面 ---
number:
Int
!
colorMap=[
1:
UIColor
.blackColor(),
2:
.orangeColor(),
3:
.blueColor()
]
init
(number initNumber:
){
.number = initNumer
.
(nibNmae:
nil
,bundle:
)
}
viewDidLoad(){
numberLabel =
UILabel
(frame:
(0,100,100))
numberLabel.center =
.view.center
numberLabel.text =
"第\(number)页"
numberLabel.textColor =
.whiteColor()
.view.addSubview(numberLabel)
.view.backgroundColor = colorMap[number]
}
@H_502_352@原文出自:
www.hangge.com
@H_502_352@转载请保留原文链接:
http://www.hangge.com/blog/cache/detail_544.html 原文链接:https://www.f2er.com/swift/324886.html
猜你在找的Swift相关文章 |