美文网首页程序员
RxSwift系列后续

RxSwift系列后续

作者: ripple_k | 来源:发表于2018-06-12 17:55 被阅读15次
  • 近来半年一直都在做小程序以及Go语言后台相关的开发工作,原本计划的更新一系列Rx的文章也很久没有更新了。今天看到RxSwift中文文档感觉真的翻译的通俗易懂,对于那些一直想入坑Rx的同学或者是想提升自己Rx功底的同学还是很有帮助的。
  • 虽然本人已经用RxSwift做过一个小项目了,但是总感觉Rx的基本功还是差太多,本人看过后真的是收获颇丰。同时也对开源社区的伟大再次小小感慨了下。
  • 虽然近期没在做iOS项目,但闲暇时还是更多的会关注iOS相关的东西,因为我知道早晚还会回到iOS开发当中去。这段的开发经历也让我对前端以及后台的技术有了更进一步的认识,总体来说还是好的。
  • 分享一下近期收获
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哈哈~
来看一下是怎么实现的吧

首先是一个命名空间的语法糖

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
    }
}
  • 时不时分享一下日常开发中的小收获还是不错的,哈哈。。
    希望继续保持,就这样...

相关文章

网友评论

    本文标题:RxSwift系列后续

    本文链接:https://www.haomeiwen.com/subject/gxsreftx.html