Skip to content

Commit

Permalink
Add ZStackLayout AspectRatioLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Sep 15, 2019
1 parent 19fbe3f commit 622da81
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class ViewController: UIViewController {

}

func makeBoxNode(_ color: UIColor) -> ASDisplayNode {
let node = ASDisplayNode()
node.backgroundColor = color
return node
}

func makeTextNode(_ string: String) -> ASTextNode {
let node = ASTextNode()
node.attributedText = NSAttributedString(string: "hello")
Expand All @@ -43,6 +49,7 @@ func makeTextNode(_ string: String) -> ASTextNode {

final class MyNode : ASDisplayNode {

private let boxNode = makeBoxNode(.lightGray)
private let textNode1 = makeTextNode("Hello")
private let textNode2 = makeTextNode("Hello")
private let textNode3 = makeTextNode("Hello")
Expand All @@ -61,6 +68,9 @@ final class MyNode : ASDisplayNode {

LayoutSpec {
VStackLayout {
AspectRatioLayout(ratio: CGSize(width: 3, height: 4)) {
boxNode
}
HStackLayout {
InsetLayout(insets: .init(top: 4, left: 4, bottom: 4, right: 4)) {
textNode1
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec
}
```

## Layout Specs

* VStackLayout
* HStackLayout
* ZStackLayout
* InsetLayout
* OverlayLayout
* BackgroundLayout
* AspectRatioLayout

## Author

Muukii <[email protected]>
2 changes: 2 additions & 0 deletions TextureSwiftSupport.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
/* Begin PBXFileReference section */
4403C6E378DD73AD896F3CF1 /* Pods_TextureSwiftSupport.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TextureSwiftSupport.framework; sourceTree = BUILT_PRODUCTS_DIR; };
473CCC32D6395F69538CB001 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
4B3ED5FA232E1CE600D68739 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
4B873C2223083FE5006170B1 /* TextureSwiftSupport.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextureSwiftSupport.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4B873C2523083FE5006170B1 /* TextureSwiftSupport.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextureSwiftSupport.h; sourceTree = "<group>"; };
4B873C2623083FE5006170B1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -90,6 +91,7 @@
4B873C1823083FE5006170B1 = {
isa = PBXGroup;
children = (
4B3ED5FA232E1CE600D68739 /* README.md */,
4B873C2423083FE5006170B1 /* TextureSwiftSupport */,
4B873C3423084045006170B1 /* Demo */,
4B873C2323083FE5006170B1 /* Products */,
Expand Down
Binary file not shown.
40 changes: 40 additions & 0 deletions TextureSwiftSupport/SpecBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ public struct HStackLayout<Content> : _ASLayoutElementType where Content : _ASLa

}

public struct ZStackLayout<Content> : _ASLayoutElementType where Content : _ASLayoutElementType {

public let childlen: Content

public init(
@ASLayoutSpecBuilder content: () -> Content
) {
self.childlen = content()
}

public func make() -> [ASLayoutElement] {
[
ASWrapperLayoutSpec(layoutElements: childlen.make())
]
}
}


public struct InsetLayout<Content> : _ASLayoutElementType where Content : _ASLayoutElementType {

public let content: Content
Expand Down Expand Up @@ -202,3 +220,25 @@ public struct BackgroundLayout<BackgroundContnt, Content> : _ASLayoutElementType

}

public struct AspectRatioLayout<Content> : _ASLayoutElementType where Content : _ASLayoutElementType {

public let content: Content
public let ratio: CGFloat

public init(ratio: CGFloat, content: () -> Content) {
self.ratio = ratio
self.content = content()
}

public init(ratio: CGSize, content: () -> Content) {
self.ratio = ratio.height / ratio.width
self.content = content()
}

public func make() -> [ASLayoutElement] {
[
ASRatioLayoutSpec(ratio: ratio, child: content.make().first!)
]
}

}

0 comments on commit 622da81

Please sign in to comment.