swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)

http://blog.csdn.net/syg90178aw/article/details/47020097

自定义view

(一)常用的写法

[objc]  view plain  copy
  1. //  自定义View  
  2.   
  3. import UIKit  
  4.   
  5. private let  KLMargin:CGFloat  = 10  
  6. private let  KLLabelHeight:CGFloat  = 30  
  7.   
  8. class CustomView: UIView {  
  9.   
  10.     // 闭包 类似oc中的block  
  11.     var buttonCallBack:(() -> ())?  
  12.       
  13.     // 重写init方法  
  14.     override init(frame: CGRect) {  
  15.         super.init(frame: frame)  
  16.           
  17.         self.backgroundColor = UIColor.orangeColor()  
  18.         let lable: UILabel = UILabel(frame: CGRectMake(KLMargin, KLMargin, KLScreenWidth - (22 * KLMargin), KLLabelHeight))  
  19.         lable.text = "我丫就是一label"  
  20.         lable.textAlignment = NSTextAlignment.Center  
  21.         lable.backgroundColor = UIColor.lightGrayColor()  
  22.         self.addSubview(lable)  
  23.           
  24.           
  25.         let button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton  
  26.         button.frame = CGRectMake(KLMargin, CGRectGetMaxY(lable.frame) + KLMargin, KLScreenWidth - (22 * KLMargin), KLLabelHeight)  
  27.         button.backgroundColor = UIColor.lightTextColor()  
  28.         button.setTitle("俺是个按钮啊", forState: UIControlState.Normal)  
  29.         button.addTarget(self, action: Selector("buttonCllick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  30.         button.layer.cornerRadius = 5  
  31.         button.clipsToBounds = true  
  32.         self.addSubview(button)  
  33.           
  34.     }  
  35.   
  36.     // 反正重写了init方法这个会根据提示自动蹦出来  
  37.     required init(coder aDecoder: NSCoder) {  
  38.         fatalError("init(coder:) has not been implemented")  
  39.     }  
  40.       
  41.     // 按钮点击事件的调用  
  42.     func buttonCllick(button: UIButton){  
  43.         if buttonCallBack != nil {  
  44.             buttonCallBack!()  
  45.         }  
  46.     }  
  47.       
  48.     // 重新绘制和oc里面效果一样(其实我也不是很明白)  
  49.     override func drawRect(rect: CGRect) {  
  50.         //self.backgroundColor = UIColor.whiteColor()  
  51.     }  
  52.   
  53. }  
在其他类的调用
[objc]  view plain  copy
  1. let  customView:CustomView = CustomView(frame: CGRectMake(080, KLScreenWidth, KLScreenWidth/2))  
  2.                 // 闭包(block)的回调  
  3.                 customView.buttonCallBack = { () -> () in  
  4.                     customView.removeFromSuperview()  
  5.                 }  
  6.                 self.view.addSubview(customView)  

(二)在一开始的时候,我是写在drawRect里的,并没有重写init方法,发现也能实现效果
[objc]  view plain  copy
  1. override func drawRect(rect: CGRect) {  
  2.         self.backgroundColor = UIColor.orangeColor()  
  3.         let lable: UILabel = UILabel(frame: CGRectMake(KLMargin, KLMargin, KLScreenWidth - (22 * KLMargin), KLLabelHeight))  
  4.         lable.text = "我丫就是一label"  
  5.         lable.textAlignment = NSTextAlignment.Center  
  6.         lable.backgroundColor = UIColor.lightGrayColor()  
  7.         self.addSubview(lable)  
  8.           
  9.           
  10.         let button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton  
  11.         button.frame = CGRectMake(KLMargin, CGRectGetMaxY(lable.frame) + KLMargin, KLScreenWidth - (22 * KLMargin), KLLabelHeight)  
  12.         button.backgroundColor = UIColor.lightTextColor()  
  13.         button.setTitle("俺是个按钮啊", forState: UIControlState.Normal)  
  14.         button.addTarget(self, action: Selector("buttonCllick:"), forControlEvents: UIControlEvents.TouchUpInside)  
  15.         button.layer.cornerRadius = 5  
  16.         button.clipsToBounds = true  
  17.         self.addSubview(button)  
  18.     }  
在其他类调用的时候,无法调用CustomView(frame:rect) 这个方法,只有像下面的代码那样调用

[objc]  view plain  copy
  1. let  customView:CustomView = CustomView()  
  2.                 customView.backgroundColor = UIColor.orangeColor()  
  3.                 customView.frame = CGRectMake(080, KLScreenWidth, KLScreenWidth/2)  
  4.                 customView.buttonCallBack = { () -> () in  
  5.                     customView.removeFromSuperview()  
  6.                 }  
  7.                 self.view.addSubview(customView)  

其实功能都能实现,但是毕竟drawRect只是绘制机制,控件的初始化,就不要在drawRect搞了,还是在init方法初始化吧
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值