将第一个childViewController.view.frame添加到ASScrollNode

时间:2018-11-13 01:02:47

标签: asyncdisplaykit

class TAUniversityNode: ASDisplayNode {

lazy var navigationBarNode: TAUniversityNavigationBarNode = {
    let node = TAUniversityNavigationBarNode()
    node.backgroundColor = UIColor.white
    return node
}()

lazy var headerNode: TAUniversityHeaderNode = {
    let node = TAUniversityHeaderNode()
    node.backgroundColor = UIColor.white
    return node
}()

lazy var scrollNode: ASScrollNode = {
    let node = ASScrollNode()
    node.view.delegate = self
    node.view.bounces = true
    node.view.isPagingEnabled = true
    node.view.showsHorizontalScrollIndicator = false
    node.view.showsVerticalScrollIndicator = false
    if #available(iOS 11.0, *) {
        node.view.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
    }
    return node
}()

var viewControllers = [TAUniversityChildVC]()

var tableViews = [ASTableView]()

var loadCategorys = [Int]()

var categorys = [String]() {
    didSet {
        headerNode.segmentsNode.tags = categorys
        lastSegment = headerNode.segmentsNode.segments.first
        lastSegment?.isSelected = true
        scrollNode.view.contentSize = CGSize(width: CGFloat(categorys.count)*scrollNode.calculatedSize.width, height: 0.0)
        scrollViewDidEndDecelerating(scrollNode.view)
    }
}

var lastSegment: ASButtonNode?

override init() {
    super.init()
    automaticallyManagesSubnodes = true
}
}

extension TAUniversityNode {

override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
    navigationBarNode.style.height = ASDimensionMake(UIScreen.navigationBarHeight)
    headerNode.style.height = ASDimensionMake(320.0)
    let headerStack = ASStackLayoutSpec.vertical()
    headerStack.children = [navigationBarNode, headerNode]
    let overlay = ASOverlayLayoutSpec(child: scrollNode, overlay: headerStack)
    return ASInsetLayoutSpec(insets: UIEdgeInsets(top: 0.0, left: 0.0, bottom: UIScreen.tabBarHeight, right: 0.0), child: overlay)
}
}

extension TAUniversityNode: UIScrollViewDelegate {

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let index = Int(scrollView.contentOffset.x/scrollNode.calculatedSize.width)

    if loadCategorys.contains(index) == false {
        let vc = TAUniversityChildVC(index)
        vc.tableNode.frame = CGRect(x: CGFloat(index)*scrollNode.calculatedSize.width, y: 0.0, width: scrollNode.calculatedSize.width, height: scrollNode.calculatedSize.height)
        scrollNode.addSubnode(vc.tableNode)
        viewControllers.append(vc)
        tableViews.append(vc.tableNode.view)
        loadCategorys.append(index)
    }
} 
}

TAUniversityChildVC是一个ASViewController,其节点为ASTableNode。但是我的第一个TAUniversityChildVC.tableNode就像这样:enter image description here,当我滚动scrollNode时,第二个TAUniversityChildVC.tableNode被添加到scrollNode,它的框架是正常的

TAUniversityNode是TAUniversityVC的节点,如下所示:

class TAUniversityVC: ASViewController {

    var categorys = [String]()

    var universityNode: TAUniversityNode {
        return node as! TAUniversityNode
    }

    init() {
        super.init(node: TAUniversityNode())
        automaticallyAdjustsScrollViewInsets = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension TAUniversityVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupInterwork()
    }

    func setupInterwork() {
        categorys.append(contentsOf: ["全部", "微课程", "文学历史", "健康生活", "文艺兴趣", "中医养生", "拳操健身", "中西烹饪"])
        universityNode.categorys = categorys     
    }
}

0 个答案:

没有答案