http://blog.csdn.net/syg90178aw/article/details/47020097
(一)常用的写法
- //自定义view
- importUIKit
- privateletKLMargin:CGFloat=10
- KLLabelHeight:CGFloat=30
- classCustomView:UIView{
- //闭包类似oc中的block
- varbuttonCallBack:(()->())?
- //重写init方法
- overrideinit(frame:CGRect){
- super.init(frame:frame)
- self.backgroundColor=UIColor.orangeColor()
- letlable:UILabel=UILabel(frame:CGRectMake(KLMargin,KLMargin,KLScreenWidth-(22*KLMargin),KLLabelHeight))
- lable.text="我丫就是一label"
- lable.textAlignment=NSTextAlignment.Center
- lable.backgroundColor=UIColor.lightGrayColor()
- self.addSubview(lable)
- letbutton:UIButton=UIButton.buttonWithType(UIButtonType.Custom)as!UIButton
- button.frame=CGRectMake(KLMargin,CGRectGetMaxY(lable.frame)+KLMargin,KLLabelHeight)
- button.backgroundColor=UIColor.lightTextColor()
- button.setTitle("俺是个按钮啊",forState:UIControlState.normal)
- button.addTarget(self,0); background-color:inherit">action:Selector("buttonCllick:"),0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
- button.layer.cornerRadius=5
- button.clipsToBounds=true
- self.addSubview(button)
- }
- //反正重写了init方法这个会根据提示自动蹦出来
- requiredinit(coderaDecoder:NSCoder){
- fatalError("init(coder:)hasnotbeenimplemented")
- }
- //按钮点击事件的调用
- funcbuttonCllick(button:UIButton){
- ifbuttonCallBack!=nil{
- buttonCallBack!()
- //重新绘制和oc里面效果一样(其实我也不是很明白)
- overridefuncdrawRect(rect:CGRect){
- //self.backgroundColor=UIColor.whiteColor()
- }
copy