/
Anchor+Builder.swift
49 lines (42 loc) · 1.64 KB
/
Anchor+Builder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#if os(iOS) || os(tvOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif
/// Act as namespace for all the builders
public final class Builder {}
/// Extend Anchor to return builders
public extension Anchor {
/// Add a ratio between width and height
func ratio(_ value: CGFloat) -> Builder.Ratio {
return Builder.Ratio(anchor: self, ratio: value)
}
/// Apply the same anchor to other views
func apply(to views: [View]) -> Builder.Apply {
return Builder.Apply(anchor: self, views: views)
}
#if os(iOS) || os(tvOS)
/// Build a paging scrollView horizontally
func pagingHorizontally(togetherWith views: [View], in scrollView: UIScrollView) -> Builder.PagingHorizontally {
var array: [View] = []
array.append(contentsOf: [item as? View].flatMap({ $0 }))
array.append(contentsOf: views)
return Builder.PagingHorizontally(scrollView: scrollView, views: array)
}
#endif
/// Add fixed spacing. The views will resize
func fixedSpacingHorizontally(togetherWith views: [View], spacing: CGFloat) -> Builder.FixedSpacingHorizontally {
var array: [View] = []
array.append(contentsOf: [item as? View].flatMap({ $0 }))
array.append(contentsOf: views)
return Builder.FixedSpacingHorizontally(views: array, spacing: spacing)
}
/// Add dynamic spacing using LayoutGuide. The spacing will resize
@available(iOS 9.0, *)
func dynamicSpacingHorizontally(togetherWith views: [View]) -> Builder.DynamicSpacingHorizontally {
var array: [View] = []
array.append(contentsOf: [item as? View].flatMap({ $0 }))
array.append(contentsOf: views)
return Builder.DynamicSpacingHorizontally(views: array)
}
}