Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Complete new layout system
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Ermolenko committed Dec 29, 2017
1 parent 7506db9 commit aad41ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
37 changes: 22 additions & 15 deletions Sources/Maker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ public final class Maker {
unowned let view: UIView

var handlers = ContiguousArray<(priority: HandlerPriority, handler: HandlerType)>()
var newSize: CGSize

var newSize: CGSize {
didSet {
newCenter.x += (newSize.width - oldValue.width) / 2.0
newCenter.y += (newSize.height - oldValue.height) / 2.0
}
}

var newCenter: CGPoint

var widthParameter: ValueParameter?
Expand Down Expand Up @@ -103,8 +110,8 @@ public final class Maker {
let height = superview.bounds.height - (insets.top + insets.bottom)
let center = CGPoint(x: insets.left + width / 2.0, y: insets.top + height / 2.0)

self.change(height: height)
self.change(width: width)
self.newSize.height = height
self.newSize.width = width
self.newCenter = center
}
handlers.append((.high, handler))
Expand Down Expand Up @@ -147,7 +154,7 @@ public final class Maker {

@discardableResult public func width(_ width: Number) -> Maker {
let handler = { [unowned self] in
self.change(width: width.value)
self.newSize.width = width.value
}
handlers.append((.high, handler))
widthParameter = ValueParameter(value: width.value)
Expand All @@ -172,10 +179,10 @@ public final class Maker {
let handler = { [unowned self] in
if view != self.view {
let width = self.relationSize(view: view, for: relationType) * multiplier.value
self.change(width: width.value)
self.newSize.width = width.value
}
else if let height = self.height {
self.change(width: height * multiplier.value)
self.newSize.width = height * multiplier.value
}
}
handlers.append((.high, handler))
Expand All @@ -192,7 +199,7 @@ public final class Maker {

@discardableResult public func height(_ height: Number) -> Maker {
let handler = { [unowned self] in
self.change(height: height.value)
self.newSize.height = height.value
}
handlers.append((.high, handler))
heightParameter = ValueParameter(value: height.value)
Expand All @@ -217,10 +224,10 @@ public final class Maker {
let handler = { [unowned self] in
if view != self.view {
let height = self.relationSize(view: view, for: relationType) * multiplier.value
self.change(height: height)
self.newSize.height = height
}
else if let width = self.width {
self.change(height: width * multiplier.value)
self.newSize.height = width * multiplier.value
}
}
handlers.append((.high, handler))
Expand Down Expand Up @@ -327,7 +334,7 @@ public final class Maker {
let handler = { [unowned self] in
let fitWidth: CGFloat = self.width ?? .greatestFiniteMagnitude
let fitSize = self.view.sizeThatFits(CGSize(width: fitWidth, height: .greatestFiniteMagnitude))
self.change(height: min(maxHeight.value, fitSize.height))
self.newSize.height = min(maxHeight.value, fitSize.height)
}
handlers.append((.high, handler))
return self
Expand All @@ -349,7 +356,7 @@ public final class Maker {
let handler = { [unowned self] in
let fitHeight: CGFloat = self.height ?? .greatestFiniteMagnitude
let fitSize = self.view.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: fitHeight))
self.change(width: min(maxWidth.value, fitSize.width))
self.newSize.width = min(maxWidth.value, fitSize.width)
}

handlers.append((.high, handler))
Expand Down Expand Up @@ -564,7 +571,7 @@ public final class Maker {
let minY = self.convertedValue(for: topParameter.relationType, with: topParameter.view) + topParameter.value
let maxY = self.convertedValue(for: relationType, with: view) - inset.value

self.change(height: maxY - minY)
self.newSize.height = maxY - minY
self.newCenter.y = maxY - self.newSize.height / 2.0
}
else if let height = self.height {
Expand Down Expand Up @@ -641,7 +648,7 @@ public final class Maker {
let minX = self.convertedValue(for: leftParameter.relationType, with: leftParameter.view) + leftParameter.value
let maxX = self.convertedValue(for: relationType, with: view) - inset.value

self.change(width: maxX - minX)
self.newSize.width = maxX - minX
self.newCenter.x = maxX - self.newSize.width / 2.0
}
else if let width = self.width {
Expand Down Expand Up @@ -890,8 +897,8 @@ public final class Maker {
private func setHighPriorityValue(_ value: CGFloat, for relationType: RelationType) {
let handler = { [unowned self] in
switch relationType {
case .width: self.change(width: value)
case .height: self.change(height: value)
case .width: self.newSize.width = value
case .height: self.newSize.height = value
default: break
}
}
Expand Down
15 changes: 1 addition & 14 deletions Sources/MakerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Maker {
func convertedValue(for type: RelationType, with view: UIView) -> CGFloat {
var rect: CGRect {
if let superview = self.view.superview, superview === view {
return CGRect(origin: .zero, size: superview.frame.size)
return CGRect(origin: .zero, size: superview.bounds.size)
}

if let supervew = self.view.superview {
Expand Down Expand Up @@ -100,16 +100,3 @@ extension Maker {
return nil
}
}

extension Maker {

func change(width: CGFloat) {
newCenter.x += (width - newSize.width) / 2.0
newSize.width = width
}

func change(height: CGFloat) {
newCenter.y += (height - newSize.height) / 2.0
newSize.height = height
}
}

0 comments on commit aad41ee

Please sign in to comment.