国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

swift閉包和OC block類型的使用

2020-03-09 17:44:45
字體:
來源:轉載
供稿:網友

之前看過一段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類型的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五华县| 金华市| 贵南县| 武乡县| 昌宁县| 和静县| 尚志市| 涿州市| 延寿县| 肥东县| 彰武县| 出国| 察哈| 都兰县| 鹤峰县| 洛川县| 唐山市| 芮城县| 吴堡县| 肥东县| 泸定县| 砚山县| 双鸭山市| 余干县| 大竹县| 西昌市| 汽车| 南宫市| 文水县| 莱州市| 海南省| 乐清市| 马公市| 商都县| 湟源县| 林芝县| 蛟河市| 汶川县| 怀宁县| 高青县| 高青县|