swift閉包和OC block類型的使用
之前看過一段swift,一直不知道OC中的block,即swift中的閉包是怎么實(shí)現(xiàn)的。今天就在網(wǎng)上搜索了一下,同時(shí)對(duì)比了一下OC中block類型的實(shí)現(xiàn)方法,然后寫了一個(gè)Demo測(cè)試一下。
使用說明:
swift版本
1.聲明類型 typealias hideShowView = (Int) -> Void
2.聲明屬性 var muFunc:hideShowView?
3.傳遞參數(shù) func didSelectedToHideView(hideFunc:@escaping (Int)->Void) { muFunc = hideFunc }
4.監(jiān)聽值的變化 func tapEvent() { muFunc!(0) }
5.使用 showView.didSelectedToHideView { (para) in NSLog("%d", para) }
6.Void 是返回值類型,Int是參數(shù)類型,hideShowView是閉包的類型名稱.第5項(xiàng)中的para是閉包的參數(shù)名,經(jīng)測(cè)試,這個(gè)參數(shù)名在使用閉包的時(shí)候可以任意修改
OC版本
.h文件
//聲明一個(gè)block類型 typedef void(^HideShowViewBlock)(int index); //聲明一個(gè)block屬性 @property (nonatomic,copy) HideShowViewBlock hideViewBlock; //傳遞參數(shù)的方法 - (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock;
.m文件
//實(shí)現(xiàn)傳遞參數(shù)的函數(shù) - (void)didHideShowViewWithBlock:(HideShowViewBlock)hideViewBlock { self.hideViewBlock = hideViewBlock; } //監(jiān)聽需要傳遞值的變化 - (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 = "測(cè)試" 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. } }
總結(jié)
以上所述是小編給大家介紹的swift閉包和OC block類型的使用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Swift 3.0基礎(chǔ)學(xué)習(xí)之?dāng)U展
擴(kuò)展是向一個(gè)已有的類、結(jié)構(gòu)體或枚舉類型添加新的功能(在swift中擴(kuò)展沒有名字)。相當(dāng)于Objective-C中Category(OC中可以有名字的,而且只能擴(kuò)展類)。這篇文章主要介紹了Swift 3.0基礎(chǔ)學(xué)習(xí)之?dāng)U展的相關(guān)資料,需要的朋友可以參考下。2017-03-03Swift 3.0將UILabel數(shù)字顏色設(shè)置為紅色的方法
這篇文章主要介紹了關(guān)于在Swift中將UILabel數(shù)字顏色設(shè)置為紅色的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03在Swift中使用Cocoa的現(xiàn)有設(shè)計(jì)模式介紹
這篇文章主要介紹了在Swift中使用Cocoa的現(xiàn)有設(shè)計(jì)模式介紹,Cocoa是蘋果公司為Mac OS X所創(chuàng)建的原生面向?qū)ο蟮腁PI,是Mac OS X上五大API之一,需要的朋友可以參考下2014-07-07判斷?ScrollView List?是否正在滾動(dòng)詳解
這篇文章主要為大家介紹了判斷?ScrollView、List?是否正在滾動(dòng)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Swift中風(fēng)味各異的類型擦除實(shí)例詳解
你也許曾聽過類型擦除,甚至也使用過標(biāo)準(zhǔn)庫(kù)提供的類型擦除類型如 AnySequence,下面這篇文章主要給大家介紹了關(guān)于Swift中風(fēng)味各異的類型擦除的相關(guān)資料,需要的朋友可以參考下2022-04-04