Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3427854
commit ad01759
Showing
6 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [] | ||
} | ||
} | ||
} | ||
} |