我正试着调整我的细胞的组成部分,但它没有正常工作。我相信,我只是不明白ASLayoutSpec背后的一些简单想法,所以我很感激这里的帮助。
我有一个单元格(当然是ASCellNode),它由ASTextNode(asTextLabel)、ASDisplayNode(asBackView,它只是带有圆角的textLabel的背景)和asImageNode(asSoundIcon)组成。
asBackView应该比asTextLabel高一点,宽一点,并且asSoundIcon应该在asBackView的右侧(比如asBackView的右侧和asSoundIcon的左侧之间的4个点),并且应该垂直居中。
我得到的是asSoundIcon不是垂直居中的,它离我想要的右边远得多。
我试过很多选择,这只是其中之一,我不明白为什么我的水平间距不起作用,以及如何垂直对齐asSoundIcon
swift - 在单元格中使用纹理(ASyncDisplayKit)-LMLPHP

override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {

    asSoundIcon.style.preferredSize = CGSize(width: 16, height: 16)
    asSoundIcon.style.maxSize = CGSize(width: 16, height: 16)
    asSoundIcon.style.minSize = CGSize(width: 16, height: 16)
    asBackView.style.minSize = CGSize(width: 30, height: 48)
    asTextLabel.style.minSize = CGSize(width: 30, height: 15)
    asTextLabel.style.maxWidth = ASDimension(unit: .points, value: screenWidth - 50)

    let asBackViewLayout = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 24), child: self.asBackView)

    let asTextLabelLayout = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 6, left: 12, bottom: 6, right: 36), child: asTextLabel)

    let asNewCenterTextLabelLayout = ASCenterLayoutSpec(horizontalPosition: .start, verticalPosition: .center, sizingOption: .minimumWidth, child: asTextLabelLayout)

    let asOverlayLayout = ASOverlayLayoutSpec(child: asNewCenterTextLabelLayout, overlay: asBackViewLayout)

    let asSoundVerticalLayout = ASRelativeLayoutSpec(horizontalPosition: .start, verticalPosition: .center, sizingOption: .minimumSize, child: asSoundIcon)

    let asSoundLayout = ASStackLayoutSpec(direction: .horizontal, spacing: 0.0, justifyContent: .start, alignItems: .start, flexWrap: .noWrap, alignContent: .start, lineSpacing: 0.0, children: [asOverlayLayout, asSoundVerticalLayout])

    return asSoundLayout
}

最佳答案

尝试删除行

let asSoundVerticalLayout = ASRelativeLayoutSpec(horizontalPosition: .start, verticalPosition: .center, sizingOption: .minimumSize, child: asSoundIcon)

换下一行:
let asSoundLayout = ASStackLayoutSpec(direction: .horizontal, spacing: 0.0, justifyContent: .start, alignItems: .center, flexWrap: .noWrap, alignContent: .start, lineSpacing: 0.0, children: [asOverlayLayout, asSoundIcon])

升级版
我试着做一个简单的project
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    viewNode.automaticallyManagesSubnodes = true
    viewNode.backgroundColor = UIColor.blue

    let asBackView = ASDisplayNode()
    asBackView.backgroundColor = UIColor.gray
    let asTextLabel = ASTextNode()
    asTextLabel.attributedText = NSAttributedString.init(string: "Cum contencio studere, omnes lunaes perdere audax, velox animalises.", attributes: [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)])
    asBackView.automaticallyManagesSubnodes = true
    asBackView.style.flexGrow = 1
    asBackView.style.flexShrink = 1
    asBackView.layoutSpecBlock = { asDisplayNode, asSizeRange in
        let asInsetSpec = ASInsetLayoutSpec(insets: UIEdgeInsetsMake(6, 12, 6, 36), child: asTextLabel)
        return asInsetSpec
    }

    let asSoundIcon = ASNetworkImageNode(cache: ASPINRemoteImageDownloader.shared(), downloader: ASPINRemoteImageDownloader.shared())
    asSoundIcon.setURL(URL.init(string: "https://www.shareicon.net/data/2017/02/09/878596_music_512x512.png"), resetToDefault: false)
    asSoundIcon.style.preferredSize = CGSize(width: 50,height: 50)
    viewNode.layoutSpecBlock = { node, constrainedSize in
        let asSoundInsetsSpec = ASInsetLayoutSpec(insets: UIEdgeInsetsMake(5, 5, 5, 5), child: asSoundIcon)
        let asStackLayout = ASStackLayoutSpec(direction: .horizontal, spacing: 0, justifyContent: .start, alignItems: .center, children: [asBackView, asSoundInsetsSpec])
        return asStackLayout
    }
}

P.S.layoutSpecBlock纹理断裂>v.2.3.4

关于swift - 在单元格中使用纹理(ASyncDisplayKit),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50619904/

10-11 00:33