Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

http://blog.csdn.net/syg90178aw/article/details/47020097 自定义view (一)常用的写法 [objc]  view plain  copy //  自定义View      import UIKit      private let  KLMargin:CGFloat  = 10   private let  KLLabelHeight:C

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

@H_618_16@ 自定义view

@H_618_16@ (一)常用的写法

@H_618_16@

@H_197_26@[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-(2@H_674_143@2*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(@H_674_143@self,0); BACkground-color:inherit">action:SELEctor("buttonCllick:"),0); BACkground-color:inherit">forControlEvents:UIControlEvents.TouchUpInside)
  22. button.layer.cornerRadius=5
  23. button.clipsToBounds=@H_674_143@true
  24. self.addSubview(button)
  25. }
  26. //反正重写了init方法这个会根据提示@L_197_7@蹦出来
  27. @L_674_8@init(coderaDecoder:NSCoder){
  28. fatalError("init(coder:)hasnotbeenimplemented")
  29. }
  30. //按钮点击事件的调用
  31. funcbuttonCllick(button:UIButton){
  32. ifbuttonCallBACk!=@H_674_143@nil{
  33. buttonCallBACk!()
  34. //重新绘制和oc里面效果一样(其实我也不是很明白)
  35. overridefuncdrawRect(rect:CGRect){
  36. //self.BACkgroundColor=UIColor.whiteColor()
  37. }
在其他类的调用
@H_874_376@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)

大佬总结

以上是大佬教程为你收集整理的swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)全部内容,希望文章能够帮你解决swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。