-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from gearmamn06/develop
Develop
- Loading branch information
Showing
13 changed files
with
522 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// | ||
// NSLayoutAnchor+Extension.swift | ||
// CoolLayout | ||
// | ||
// Created by Sudo.park on 2019/11/30. | ||
// | ||
|
||
import UIKit | ||
|
||
|
||
fileprivate extension NSLayoutConstraint { | ||
|
||
private var shouldActive: Bool { | ||
if let firstView = self.firstItem as? UIView { | ||
return firstView.shouldActiveWhenConstraintMade | ||
} | ||
return false | ||
} | ||
|
||
func activeOrNot() -> NSLayoutConstraint { | ||
if shouldActive { | ||
self.isActive = true | ||
} | ||
return self | ||
} | ||
} | ||
|
||
|
||
// MARK: - Equal operation | ||
extension NSLayoutXAxisAnchor { | ||
|
||
@discardableResult | ||
public func equal(_ otherAnchor: NSLayoutXAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(equalTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func equal(_ half: HalfNSLayoutConstraint<NSLayoutXAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(equalTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutYAxisAnchor { | ||
|
||
@discardableResult | ||
public func equal(_ otherAnchor: NSLayoutYAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(equalTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func equal(_ half: HalfNSLayoutConstraint<NSLayoutYAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(equalTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutDimension { | ||
|
||
@discardableResult | ||
public func equal(_ constant: CGFloat) -> NSLayoutConstraint { | ||
return self.constraint(equalToConstant: constant) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func equal(_ otherAnchor: NSLayoutDimension) -> NSLayoutConstraint { | ||
return self.constraint(equalTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func equal(_ half: HalfNSLayoutConstraint<NSLayoutDimension>) -> NSLayoutConstraint { | ||
guard let layoutDimension = half.referenceAnchor as? NSLayoutDimension else { | ||
fatalError() | ||
} | ||
return self.constraint(equalTo: layoutDimension, multiplier: half.multiplier, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
// MARK: - greaterThan operation | ||
extension NSLayoutXAxisAnchor { | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual otherAnchor: NSLayoutXAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual half: HalfNSLayoutConstraint<NSLayoutXAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutYAxisAnchor { | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual otherAnchor: NSLayoutYAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual half: HalfNSLayoutConstraint<NSLayoutYAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutDimension { | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual constant: CGFloat) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualToConstant: constant) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual otherAnchor: NSLayoutDimension) -> NSLayoutConstraint { | ||
return self.constraint(greaterThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func greaterThan(orEqual half: HalfNSLayoutConstraint<NSLayoutDimension>) -> NSLayoutConstraint { | ||
guard let layoutDimension = half.referenceAnchor as? NSLayoutDimension else { | ||
fatalError() | ||
} | ||
return self.constraint(greaterThanOrEqualTo: layoutDimension, | ||
multiplier: half.multiplier, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
// MARK: - lessThan operation | ||
extension NSLayoutXAxisAnchor { | ||
|
||
@discardableResult | ||
public func lessThan(orEqual otherAnchor: NSLayoutXAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func lessThan(orEqual half: HalfNSLayoutConstraint<NSLayoutXAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutYAxisAnchor { | ||
|
||
@discardableResult | ||
public func lessThan(orEqual otherAnchor: NSLayoutYAxisAnchor) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func lessThan(orEqual half: HalfNSLayoutConstraint<NSLayoutYAxisAnchor>) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualTo: half.referenceAnchor, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} | ||
|
||
extension NSLayoutDimension { | ||
|
||
@discardableResult | ||
public func lessThan(orEqual constant: CGFloat) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualToConstant: constant) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func lessThan(orEqual otherAnchor: NSLayoutDimension) -> NSLayoutConstraint { | ||
return self.constraint(lessThanOrEqualTo: otherAnchor) | ||
.activeOrNot() | ||
} | ||
|
||
@discardableResult | ||
public func lessThan(orEqual half: HalfNSLayoutConstraint<NSLayoutDimension>) -> NSLayoutConstraint { | ||
guard let layoutDimension = half.referenceAnchor as? NSLayoutDimension else { | ||
fatalError() | ||
} | ||
return self.constraint(lessThanOrEqualTo: layoutDimension, | ||
multiplier: half.multiplier, constant: half.constant) | ||
.activeOrNot() | ||
} | ||
} |
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 |
---|---|---|
|
@@ -57,6 +57,4 @@ public extension NSLayoutConstraint { | |
|
||
return newConstraint | ||
} | ||
|
||
} | ||
|
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,60 @@ | ||
// | ||
// CircleViewController.swift | ||
// CoolLayout_Example | ||
// | ||
// Created by Sudo.park on 2019/11/30. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
import CoolLayout | ||
|
||
|
||
class CircleViewController: UIViewController { | ||
|
||
private let circleView = UIView { | ||
$0.backgroundColor = UIColor.blue | ||
} | ||
|
||
private let label = UILabel { | ||
$0.textColor = UIColor.black | ||
$0.textAlignment = .center | ||
$0.numberOfLines = 0 | ||
$0.font = UIFont.boldSystemFont(ofSize: 18) | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
self.view.apply { | ||
$0.backgroundColor = UIColor.white | ||
$0.addSubview(circleView) | ||
$0.addSubview(label) | ||
} | ||
|
||
self.circleView | ||
.activate { | ||
$0.centerXAnchor.equal(self.view.centerXAnchor) | ||
$0.centerYAnchor.equal(self.view.centerYAnchor) | ||
$0.widthAnchor.equal(100) | ||
$0.heightAnchor.equal(100) | ||
} | ||
self.circleView | ||
.apply { | ||
$0.layoutIfNeeded() | ||
$0.clipsToBounds = true | ||
$0.layer.cornerRadius = $0.frame.width / 2 | ||
} | ||
|
||
self.label.apply { | ||
$0.text = "This method builds faster than using a custom operator." | ||
} | ||
.activate { | ||
$0.centerXAnchor.equal(self.view.centerXAnchor) | ||
$0.topAnchor.equal(self.circleView.bottomAnchor + 50) | ||
$0.leadingAnchor.greaterThan(orEqual: self.view.leadingAnchor + 20) | ||
$0.trailingAnchor.lessThan(orEqual: self.view.trailingAnchor + -20) | ||
} | ||
} | ||
} |
Oops, something went wrong.