这篇文章主要介绍了swift闭包和OC block类型的使用,需要的朋友可以参考下
之前看过一段swift,一直不知道OC中的block,即swift中的闭包是怎么实现的。今天就在网上搜索了一下,同时对比了一下OC中block类型的实现方法,然后写了一个Demo测试一下。
使用说明:
swift版本
1.声明类型 typealias hideShowView = (Int) -> Void
2.声明属性 var muFunc:hideShowView?
3.传递参数 func didSelectedToHideView(hideFunc:@escaping (Int)->Void) { muFunc = hideFunc }
4.监听值的变化 func tapEvent() { muFunc!(0) }
5.使用 showView.didSelectedToHideView { (para) in NSLog("%d", para) }
6.Void 是返回值类型,Int是参数类型,hideShowView是闭包的类型名称.第5项中的para是闭包的参数名,经测试,这个参数名在使用闭包的时候可以任意修改
OC版本
.h文件
//声明一个block类型
typedef void(^HideShowViewBlock)(int index);
//声明一个block属性
@property (nonatomic,copy) HideShowViewBlock hideViewBlock;
//传递参数的方法
- (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock;
.m文件
//实现传递参数的函数
- (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock
{
self.hideViewBlock = hideViewBlock;
}
//监听需要传递值的变化
- (void)tapEvent
{
self.hideViewBlock(0);
}
swift 闭包 Demo的代码
class ShowView: UIView
{
typealias hideShowView = (Int) -> Void
var muFunc:hideShowView?
private var viewFram:CGRect?
override init(frame:CGRect )
{
super.init(frame: frame)
self.viewFram = frame
self.backgroundColor = UIColor.gray
self.createUI()
}
func createUI()
{
var centerLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (self.viewFram?.width)!, height: 20))
centerLabel.center = self.center
centerLabel.text = "测试"
centerLabel.textColor = UIColor.white
centerLabel.textAlignment = NSTextAlignment.center
centerLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
self.addSubview(centerLabel)
let tap = UITapGestureRecognizer.init(target: self, action: #selector(ShowView.tapEvent))
tap.cancelsTouchesInView = false
self.addGestureRecognizer(tap)
}
func tapEvent()
{
muFunc!(0)
}
func didSelectedToHideView(hideFunc:@escaping (Int)->Void)
{
muFunc = hideFunc
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController
{
let WIDTH = UIScreen.main.bounds.size.width
let HEIGHT = UIScreen.main.bounds.size.height
override func viewDidLoad()
{
super.viewDidLoad()
}
@IBAction func selectedToDoSomething(_ sender: UIButton)
{
let showView = ShowView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH/2, height: WIDTH/2))
showView.center = self.view.center
showView.didSelectedToHideView { (para) in
NSLog("%d", para)
}
self.view.addSubview(showView)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
总结
以上所述是小编给大家介绍的swift闭包和OC block类型的使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
本文标题为:swift闭包和OC block类型的使用
基础教程推荐
- R语言基于Keras的MLP神经网络及环境搭建 2022-12-10
- asm基础——汇编指令之in/out指令 2023-07-06
- ruby-on-rails-使用Nginx的Rails的多阶段环境 2023-09-21
- R语言数可视化Split violin plot小提琴图绘制方法 2022-12-10
- swift 字符串String的使用方法 2023-07-05
- Go web部署报错panic: listen tcp xxxxxxx:8090: bind: cannot assign requested address 2023-09-05
- UEFI开发基础HII代码示例 2023-07-07
- R包ggtreeExtra绘制进化树 2022-12-14
- swift版webview加载网页进度条效果 2023-07-05
- R语言-如何将科学计数法表示的数字转化为文本 2022-11-23