WWDC推出了SwiftUI、Combine,仅Swift可用~为了能顺利的也吃上SwiftUI,我也打算将本身的项目先从OC慢慢迁移到Swift,因此~一块儿从混编开始!git
正常建立文件,语言选swift github
上一步,点完next,系统会提示以下弹框。点create Bridging Headerswift
#import "OCAndSwift-Swift.h" //项目名称-Swift.h
复制代码
点击进去,能够看到我刚刚建的文件的,以下所示,有初始化的方法,和我暴露在外面的方法~全部swift文件都会在这个文件里面被“转化”成OCbash
ZTSwiftView *view = [[ZTSwiftView alloc]init];
[self.view addSubview:view];
__weak typeof(view) weakView = view;
//点了确认后执行此block
view.selectColorBlockSwift = ^(NSString * _Nonnull str) {
__strong typeof(weakView) strongView = weakView;
//将string赋值给view的button
[strongView reloadBtnTitleWithTitle:str];
};
复制代码
其中reloadBtnTitleWithTitle方法是swift里面的方法,swift方法想被oc调用,前面需带objc,以下ui
@objc public func reloadBtnTitle(title:NSString){
confirmButton.backgroundColor = .white
confirmButton.setTitle(title as String, for: .normal)
}
复制代码
let ocView = ZTOCView()
ocView.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
contentView.addSubview(ocView)
weak var weakSelf = self
//点ocView中间的view后的block
ocView.changeColorBlock = {(color : UIColor?) -> Void in
weakSelf?.confirmButton.backgroundColor = color
weakSelf?.confirmButton.setTitle("肯定", for: .normal)
}
复制代码
效果图以下,是个有些丑的demo~spa