在iOS开发中,如果创建一个自定义的组件通常可以通过继承UIView来实现。
import UIKit
enum ScoreType{
case Common //普通分数面板
case Best //最高分面板
}
protocol ScoreViewProtocol{
func changeScore(value s:Int)
}
class ScoreView:UIView, ScoreViewProtocol
{
var label:UILabel!
let defaultFrame = CGRectMake(0,0,100,30)
var stype:String! //显示”最高分“还是”分数“
var score:Int = 0{
didSet{
label.text = "\(stype):\(score)"
}
}
init(stype:ScoreType)
{
label = UILabel(frame:defaultFrame)
label.textAlignment = NSTextAlignment.Center
super.init(frame:defaultFrame)
self.stype = (stype == ScoreType.Common ? "分数":"最高分")
backgroundColor = UIColor.orangeColor()
label.font = UIFont(name:"微软雅黑", size:16)
label.textColor = UIColor.whiteColor()
self.addSubview(label)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}