- 近來半年一直都在做小程序以及Go語言后臺相關(guān)的開發(fā)工作,原本計劃的更新一系列Rx的文章也很久沒有更新了。今天看到RxSwift中文文檔感覺真的翻譯的通俗易懂,對于那些一直想入坑Rx的同學(xué)或者是想提升自己Rx功底的同學(xué)還是很有幫助的。
- 雖然本人已經(jīng)用RxSwift做過一個小項目了,但是總感覺Rx的基本功還是差太多,本人看過后真的是收獲頗豐。同時也對開源社區(qū)的偉大再次小小感慨了下。
- 雖然近期沒在做iOS項目,但閑暇時還是更多的會關(guān)注iOS相關(guān)的東西,因為我知道早晚還會回到iOS開發(fā)當(dāng)中去。這段的開發(fā)經(jīng)歷也讓我對前端以及后臺的技術(shù)有了更進一步的認(rèn)識,總體來說還是好的。
- 分享一下近期收獲
class VCutCollectionViewCell: UICollectionViewCell {
let imageView = UIImageView()
let playButton = UIButton(type: .custom)
let disposeBag = DisposeBag()
override init(frame: CGRect) {
super.init(frame: frame)
imageView
.soap.adhere(toSuperView: contentView)
.soap.config { (imageView) in
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = true
imageView.isUserInteractionEnabled = true
}
.soap.layout { (make) in
make.edges.equalTo(contentView)
}
playButton
.soap.adhere(toSuperView: contentView)
.soap.config { (button) in
button.frame.size = CGSize(width: 60, height: 60)
button.setBackgroundImage(#imageLiteral(resourceName: "lightbox_play"), for: UIControlState())
button.layer.shadowOffset = CGSize(width: 1, height: 1)
button.layer.shadowColor = UIColor.gray.cgColor
button.layer.masksToBounds = false
button.layer.shadowOpacity = 0.8
}
.soap.layout { (make) in
make.center.equalTo(contentView)
}
.rx.tap.asObservable()
.subscribe(onNext: { (_) in
print("tap...")
})
.disposed(by: disposeBag)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
這樣寫代碼是不是很爽?? O(∩_∩)O哈哈~
來看一下是怎么實現(xiàn)的吧
首先是一個命名空間的語法糖
public protocol NamespaceWrappable {
associatedtype SoapWrapperType
var soap: SoapWrapperType { get }
static var soap: SoapWrapperType.Type { get }
}
public extension NamespaceWrappable {
var soap: NamespaceWrapper<Self> {
return NamespaceWrapper(value: self)
}
static var soap: NamespaceWrapper<Self>.Type {
return NamespaceWrapper.self
}
}
public struct NamespaceWrapper<T> {
public let wrappedValue: T
public init(value: T) {
self.wrappedValue = value
}
}
接下來就是針對這個命名空間里的UIView的擴展啦。。。
extension UIView: NamespaceWrappable { }
extension NamespaceWrapper where T: UIView {
public func adhere(toSuperView: UIView) -> T {
toSuperView.addSubview(wrappedValue)
return wrappedValue
}
@discardableResult
public func layout(snapKitMaker: (ConstraintMaker) -> Void) -> T {
wrappedValue.snp.makeConstraints { (make) in
snapKitMaker(make)
}
return wrappedValue
}
@discardableResult
public func config(_ config: (T) -> Void) -> T {
config(wrappedValue)
return wrappedValue
}
}
- 時不時分享一下日常開發(fā)中的小收獲還是不錯的,哈哈。。
希望繼續(xù)保持,就這樣...