Skip to content

Commit

Permalink
Add Ratio. Use convenience init
Browse files Browse the repository at this point in the history
  • Loading branch information
onmyway133 committed May 17, 2017
1 parent 3427854 commit ad01759
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Anchors.xcodeproj/project.pbxproj
Expand Up @@ -11,6 +11,8 @@
D29683501EBD19BE001E62CB /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D296834E1EBD19BE001E62CB /* Tests.swift */; };
D296835E1EBDBA37001E62CB /* TestCase+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D296835D1EBDBA37001E62CB /* TestCase+Extensions.swift */; };
D296835F1EBDBA37001E62CB /* TestCase+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D296835D1EBDBA37001E62CB /* TestCase+Extensions.swift */; };
D2B5CBC91ECCF9E7003F9D5F /* Builder+Ratio.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2B5CBC81ECCF9E7003F9D5F /* Builder+Ratio.swift */; };
D2B5CBCA1ECCF9E7003F9D5F /* Builder+Ratio.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2B5CBC81ECCF9E7003F9D5F /* Builder+Ratio.swift */; };
D2D43D441EC7450300065343 /* Anchor+Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D43D2E1EC7450300065343 /* Anchor+Attributes.swift */; };
D2D43D451EC7450300065343 /* Anchor+Attributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D43D2E1EC7450300065343 /* Anchor+Attributes.swift */; };
D2D43D461EC7450300065343 /* Anchor+Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D43D2F1EC7450300065343 /* Anchor+Configuration.swift */; };
Expand Down Expand Up @@ -75,6 +77,7 @@
/* Begin PBXFileReference section */
D296834E1EBD19BE001E62CB /* Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
D296835D1EBDBA37001E62CB /* TestCase+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TestCase+Extensions.swift"; sourceTree = "<group>"; };
D2B5CBC81ECCF9E7003F9D5F /* Builder+Ratio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Builder+Ratio.swift"; sourceTree = "<group>"; };
D2D43D2E1EC7450300065343 /* Anchor+Attributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Anchor+Attributes.swift"; sourceTree = "<group>"; };
D2D43D2F1EC7450300065343 /* Anchor+Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Anchor+Configuration.swift"; sourceTree = "<group>"; };
D2D43D301EC7450300065343 /* Anchor+Constraints.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Anchor+Constraints.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -165,6 +168,7 @@
D2D43D3B1EC7450300065343 /* Builder+DynamicSpacingHorizontally.swift */,
D2D43D3C1EC7450300065343 /* Builder+FixedSpacingHorizontally.swift */,
D2D43D3D1EC7450300065343 /* Builder+PagingHorizontally.swift */,
D2B5CBC81ECCF9E7003F9D5F /* Builder+Ratio.swift */,
);
path = Builder;
sourceTree = "<group>";
Expand Down Expand Up @@ -441,6 +445,7 @@
D2D43D5A1EC7450300065343 /* Builder+Apply.swift in Sources */,
D2D43D661EC7450300065343 /* View+Extensions.swift in Sources */,
D2D43D4C1EC7450300065343 /* Anchor+Find.swift in Sources */,
D2B5CBC91ECCF9E7003F9D5F /* Builder+Ratio.swift in Sources */,
D2D43D4E1EC7450300065343 /* Anchor+Helper.swift in Sources */,
D2D43D461EC7450300065343 /* Anchor+Configuration.swift in Sources */,
D2D43D541EC7450300065343 /* Anchor.swift in Sources */,
Expand Down Expand Up @@ -477,6 +482,7 @@
D2D43D5B1EC7450300065343 /* Builder+Apply.swift in Sources */,
D2D43D671EC7450300065343 /* View+Extensions.swift in Sources */,
D2D43D4D1EC7450300065343 /* Anchor+Find.swift in Sources */,
D2B5CBCA1ECCF9E7003F9D5F /* Builder+Ratio.swift in Sources */,
D2D43D4F1EC7450300065343 /* Anchor+Helper.swift in Sources */,
D2D43D471EC7450300065343 /* Anchor+Configuration.swift in Sources */,
D2D43D551EC7450300065343 /* Anchor.swift in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions AnchorsTests/Tests.swift
Expand Up @@ -136,6 +136,21 @@ class Tests: XCTestCase {
$0.firstAttribute == .height && $0.multiplier == 1.5 }))
}

func testRatio2() {
let view = View()
let constraints = view.anchor.width.equal.to(10).constraints()
+ view.anchor.height.ratio(1.5).constraints()

XCTAssertEqual(constraints.count, 2)
XCTAssertTrue(constraints.contains(where: {
$0.firstAttribute == .width && $0.constant == 10 }))

XCTAssertTrue(constraints.contains(where: {
$0.firstAttribute == .height && $0.constant == 0 }))
XCTAssertTrue(constraints.contains(where: {
$0.firstAttribute == .height && $0.multiplier == 1.5 }))
}

func testActivate() {
let superview = View(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
let view = View()
Expand Down
4 changes: 4 additions & 0 deletions Sources/Anchor/Anchor+Helper.swift
Expand Up @@ -9,4 +9,8 @@ extension Anchor {
let pin = pins.filter({ $0.attribute == attribute }).first
pin?.constant = constant
}

func exists(_ attribute: NSLayoutAttribute) -> Bool {
return pins.index(where: { $0.attribute == attribute }) != nil
}
}
13 changes: 9 additions & 4 deletions Sources/Anchor/Anchor.swift
Expand Up @@ -35,13 +35,18 @@ public class Anchor: ConstraintProducer {
var toValue: To = .none

/// Init with View
init(view: View) {
self.item = view
convenience init(view: View) {
self.init(item: view)
}

/// Init with Layout Guide
@available(iOS 9, *)
init(layoutGuide: LayoutGuide) {
self.item = layoutGuide
convenience init(layoutGuide: LayoutGuide) {
self.init(item: layoutGuide)
}

// Init with Item
init(item: AnyObject) {
self.item = item
}
}
5 changes: 5 additions & 0 deletions Sources/Builder/Anchor+Builder.swift
Expand Up @@ -10,6 +10,11 @@ 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)
Expand Down
36 changes: 36 additions & 0 deletions Sources/Builder/Builder+Ratio.swift
@@ -0,0 +1,36 @@
#if os(iOS) || os(tvOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif

public extension Builder {
public class Ratio: ConstraintProducer {

let sourceAnchor: Anchor
let ratio: CGFloat

init(anchor: Anchor, ratio: CGFloat) {
self.sourceAnchor = anchor
self.ratio = ratio
}

public func constraints() -> [NSLayoutConstraint] {
// Only care about width and height, so create new Anchor, because accessing anchor
// on the same object will add more pins
if sourceAnchor.exists(.width) {
return Anchor(item: sourceAnchor.item).width
.equal
.to(Anchor(item: sourceAnchor.item).height)
.multiplier(ratio).constraints()
} else if sourceAnchor.exists(.height) {
return Anchor(item: sourceAnchor.item).height
.equal
.to(Anchor(item: sourceAnchor.item).width)
.multiplier(ratio).constraints()
} else {
return []
}
}
}
}

0 comments on commit ad01759

Please sign in to comment.