在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现。下面以一个记分牌组件为例,演示了组件的创建和使用,以及枚举、协议等相关知识的学习。
组件使用:
31
原文链接:https://www.f2er.com/swift/324600.html
效果图如下:
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
@H_502_81@
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import
UIKit
enum
scoreType
{
case
Common
//普通分数面板
Best
//最高分面板
}
protocol
scoreViewProtocol
{
}
{
var
label:
UILabel
!
let
defaultFrame =
CGRectMake
(0,100,30)
score:
= 0{
didSet
{
label.text =
"\(stype):\(score)"
}
}
init
(stype:
)
{
label =
(frame:defaultFrame)
label.textAlignment =
NSTextAlignment
.
Center
super
.
(frame:defaultFrame)
self
.stype = (stype ==
Common
?
"分数"
:
"最高分"
)
backgroundColor =
UIColor
.orangeColor()
label.font =
UIFont
(name:
"微软雅黑"
label.textColor =
.whiteColor()
.addSubview(label)
}
(coder: aDecoder)
}
//实现协议中的方法
)
{
score = s
}
}
|
组件使用:
ViewController
UIViewController
score:
!
bestscore:
!
override
viewDidLoad() {
.viewDidLoad()
setupscoreLabels();
}
setupscoreLabels()
{
score =
)
.view.addSubview(score)
bestscore.frame.origin.x = 170
bestscore.frame.origin.y = 80
.view.addSubview(bestscore)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}
|