swiftasyncdisplaykit

Using Texture (ASyncDisplayKit) in cells


I'm trying to align components of my cell, but it doesn't work as should. I believe, I just don't understand some simple ideas behind ASLayoutSpec, so I'd appreciate any help here.

I have a cell (ASCellNode, of course) that consists of ASTextNode (asTextLabel), ASDisplayNode (asBackView, it's just a background for textLabel with rounded corners), and asImageNode (asSoundIcon).

asBackView should be a little higher and wider than asTextLabel (it works), and asSoundIcon should be on the right side of asBackView (like, 4 points between the right side of asBackView and the left side of asSoundIcon), and it should be vertically centered.

What I have is asSoundIcon is not vertically centered, and it is much farther to the right that I wanted.

I've tried a lot of options, this is just one of them, and I don't understand why my horizontal spacing doesn't work, and how to have vertically aligned asSoundIcon.

sorry for the colors

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
}

Solution

  • Try remove line

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

    And change next line to it:

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

    UPD

    I try to do a simple 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 broken on Texture > v. 2.3.4