加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:05:03 所属栏目:百科 来源:网络整理
导读:http://blog.csdn.net/syg90178aw/article/details/47020097 自定义view (一)常用的写法 [objc] view plain copy //自定义View importUIKit private let KLMargin :CGFloat= 1 0 KLLabelHeight :CGFloat= 3 0 class CustomView :UIView{ //闭包类似oc中的blo

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

自定义view

(一)常用的写法

[objc] view plain copy
  1. //自定义View
  2. importUIKit
  3. privateletKLMargin:CGFloat=10
  4. KLLabelHeight:CGFloat=30
  5. classCustomView:UIView{
  6. //闭包类似oc中的block
  7. varbuttonCallBack:(()->())?
  8. //重写init方法
  9. overrideinit(frame:CGRect){
  10. super.init(frame:frame)
  11. self.backgroundColor=UIColor.orangeColor()
  12. letlable:UILabel=UILabel(frame:CGRectMake(KLMargin,KLMargin,KLScreenWidth-(22*KLMargin),KLLabelHeight))
  13. lable.text="我丫就是一label"
  14. lable.textAlignment=NSTextAlignment.Center
  15. lable.backgroundColor=UIColor.lightGrayColor()
  16. self.addSubview(lable)
  17. letbutton:UIButton=UIButton.buttonWithType(UIButtonType.Custom)as!UIButton
  18. button.frame=CGRectMake(KLMargin,CGRectGetMaxY(lable.frame)+KLMargin,KLLabelHeight)
  19. button.backgroundColor=UIColor.lightTextColor()
  20. button.setTitle("俺是个按钮啊",forState:UIControlState.Normal)
  21. button.addTarget(self,0); background-color:inherit">action:Selector("buttonCllick:"),0); background-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
  22. button.layer.cornerRadius=5
  23. button.clipsToBounds=true
  24. self.addSubview(button)
  25. }
  26. //反正重写了init方法这个会根据提示自动蹦出来
  27. requiredinit(coderaDecoder:NSCoder){
  28. fatalError("init(coder:)hasnotbeenimplemented")
  29. }
  30. //按钮点击事件的调用
  31. funcbuttonCllick(button:UIButton){
  32. ifbuttonCallBack!=nil{
  33. buttonCallBack!()
  34. //重新绘制和oc里面效果一样(其实我也不是很明白)
  35. overridefuncdrawRect(rect:CGRect){
  36. //self.backgroundColor=UIColor.whiteColor()
  37. }
在其他类的调用
copy
    letcustomView:CustomView=CustomView(frame:CGRectMake(0,80,KLScreenWidth,KLScreenWidth/2))
  1. //闭包(block)的回调
  2. customView.buttonCallBack={()->()in
  3. customView.removeFromSuperview()
  4. self.view.addSubview(customView)

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

copy
    customView:CustomView=CustomView()
  1. customView.backgroundColor=UIColor.orangeColor()
  2. customView.frame=CGRectMake(0,KLScreenWidth/2)
  3. customView.buttonCallBack={()->()in
  4. customView.removeFromSuperview()
  5. self.view.addSubview(customView)

其实功能都能实现,但是毕竟drawRect只是绘制机制,控件的初始化,就不要在drawRect搞了,还是在init方法初始化吧

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读