// // RDReaderContentCell.swift // RDReaderDemo // // Created by yangsq on 2021/8/7. // // 文件职责:UICollectionViewCell 薄壳宿主,作为滚动模式下页面内容的容器。 // 该 cell 本身不包含业务逻辑,仅持有 containerView 并通过自动布局将其填满。 // 真正的内容视图由 RDReaderDataSource.pageContentView() 提供并赋值给 containerView。 // // 架构位置:RDReaderView 四层架构中第三层的 cell 组件,被 UICollectionView 使用。 // import UIKit /// 内容 cell 薄壳宿主 /// 作为 UICollectionView 滚动模式下的页面容器。 /// 通过 ``containerView`` 属性持有实际内容视图,并在 layoutSubviews 中 /// 将内容视图的 frame 设置为与 contentView 等大,实现自动填满。 class RDReaderContentCell: UICollectionViewCell { /// 内部持有的内容视图引用 private var _containerView: UIView? = nil /// 内容视图属性 /// 设置时会自动将旧视图移除、新视图添加到 contentView 中 /// 读取时返回当前持有的内容视图 var containerView: UIView? { set { guard newValue !== _containerView else { return } _containerView?.removeFromSuperview() _containerView = newValue if let containerView = _containerView { contentView.addSubview(containerView) setNeedsLayout() } } get { return _containerView } } /// 布局子视图时调用 /// 将 containerView 的 frame 设置为与 contentView 等大,实现自动填满 override func layoutSubviews() { super.layoutSubviews() _containerView?.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height) } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }