Commit 0d43dc6e by Daniel Dahan

development: updated Layout and Grid calculations

parent c1c421de
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'Material' s.name = 'Material'
s.version = '2.3.22' s.version = '2.4.0'
s.license = 'BSD-3-Clause' s.license = 'BSD-3-Clause'
s.summary = 'Material is an animation and graphics framework that is used to create beautiful applications.' s.summary = 'Material is an animation and graphics framework that is used to create beautiful applications.'
s.homepage = 'http://materialswift.com' s.homepage = 'http://materialswift.com'
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>FMWK</string> <string>FMWK</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>2.3.22</string> <string>2.4.0</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -93,7 +93,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -93,7 +93,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
much greater flexibility than the contentsGravity property in much greater flexibility than the contentsGravity property in
terms of how the image is cropped and stretched. terms of how the image is cropped and stretched.
*/ */
@IBInspectable open var contentsRect: CGRect { @IBInspectable
open var contentsRect: CGRect {
get { get {
return visualLayer.contentsRect return visualLayer.contentsRect
} }
...@@ -106,7 +107,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -106,7 +107,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
A CGRect that defines a stretchable region inside the visualLayer A CGRect that defines a stretchable region inside the visualLayer
with a fixed border around the edge. with a fixed border around the edge.
*/ */
@IBInspectable open var contentsCenter: CGRect { @IBInspectable
open var contentsCenter: CGRect {
get { get {
return visualLayer.contentsCenter return visualLayer.contentsCenter
} }
...@@ -120,7 +122,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -120,7 +122,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
dimensions of the visualLayer's contents property and the size dimensions of the visualLayer's contents property and the size
of the view. By default, this value is set to the Screen.scale. of the view. By default, this value is set to the Screen.scale.
*/ */
@IBInspectable open var contentsScale: CGFloat { @IBInspectable
open var contentsScale: CGFloat {
get { get {
return visualLayer.contentsScale return visualLayer.contentsScale
} }
...@@ -137,7 +140,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -137,7 +140,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
} }
/// Determines how content should be aligned within the visualLayer's bounds. /// Determines how content should be aligned within the visualLayer's bounds.
@IBInspectable open var contentsGravity: String { @IBInspectable
open var contentsGravity: String {
get { get {
return visualLayer.contentsGravity return visualLayer.contentsGravity
} }
...@@ -157,7 +161,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -157,7 +161,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
} }
/// A reference to EdgeInsets. /// A reference to EdgeInsets.
@IBInspectable open var contentEdgeInsets: UIEdgeInsets { @IBInspectable
open var contentEdgeInsets: UIEdgeInsets {
get { get {
return grid.contentEdgeInsets return grid.contentEdgeInsets
} }
...@@ -174,7 +179,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -174,7 +179,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
} }
/// A wrapper around grid.interimSpace. /// A wrapper around grid.interimSpace.
@IBInspectable open var interimSpace: InterimSpace { @IBInspectable
open var interimSpace: InterimSpace {
get { get {
return grid.interimSpace return grid.interimSpace
} }
...@@ -184,7 +190,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -184,7 +190,8 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
} }
/// A property that accesses the backing layer's background /// A property that accesses the backing layer's background
@IBInspectable open override var backgroundColor: UIColor? { @IBInspectable
open override var backgroundColor: UIColor? {
didSet { didSet {
layer.backgroundColor = backgroundColor?.cgColor layer.backgroundColor = backgroundColor?.cgColor
} }
...@@ -283,7 +290,6 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable { ...@@ -283,7 +290,6 @@ open class CollectionReusableView: UICollectionReusableView, Pulseable {
*/ */
open func prepare() { open func prepare() {
contentScaleFactor = Screen.scale contentScaleFactor = Screen.scale
pulseAnimation = .none
prepareVisualLayer() prepareVisualLayer()
preparePulse() preparePulse()
} }
...@@ -293,6 +299,7 @@ extension CollectionReusableView { ...@@ -293,6 +299,7 @@ extension CollectionReusableView {
/// Prepares the pulse motion. /// Prepares the pulse motion.
fileprivate func preparePulse() { fileprivate func preparePulse() {
pulse = Pulse(pulseView: self, pulseLayer: visualLayer) pulse = Pulse(pulseView: self, pulseLayer: visualLayer)
pulseAnimation = .none
} }
/// Prepares the visualLayer property. /// Prepares the visualLayer property.
......
...@@ -204,16 +204,13 @@ public struct Grid { ...@@ -204,16 +204,13 @@ public struct Grid {
} }
} }
canvas.setNeedsLayout() let count = views.count
canvas.layoutIfNeeded()
guard 0 < canvas.width && 0 < canvas.height else { guard 0 < count else {
return return
} }
let count = views.count guard 0 < canvas.width && 0 < canvas.height else {
guard 0 < count else {
return return
} }
......
...@@ -113,11 +113,9 @@ open class Layer: CAShapeLayer { ...@@ -113,11 +113,9 @@ open class Layer: CAShapeLayer {
open override var cornerRadius: CGFloat { open override var cornerRadius: CGFloat {
didSet { didSet {
layoutShadowPath() layoutShadowPath()
if .circle == shapePreset {
shapePreset = .none shapePreset = .none
} }
} }
}
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
......
...@@ -639,6 +639,8 @@ extension Layout { ...@@ -639,6 +639,8 @@ extension Layout {
public class func width(parent: UIView, child: UIView, width: CGFloat = 0) { public class func width(parent: UIView, child: UIView, width: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -650,6 +652,8 @@ extension Layout { ...@@ -650,6 +652,8 @@ extension Layout {
public class func height(parent: UIView, child: UIView, height: CGFloat = 0) { public class func height(parent: UIView, child: UIView, height: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -682,6 +686,10 @@ extension Layout { ...@@ -682,6 +686,10 @@ extension Layout {
} }
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
} }
for child in children {
child.setNeedsLayout()
child.layoutIfNeeded()
}
} }
/** /**
...@@ -703,6 +711,10 @@ extension Layout { ...@@ -703,6 +711,10 @@ extension Layout {
} }
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
} }
for child in children {
child.setNeedsLayout()
child.layoutIfNeeded()
}
} }
/** /**
...@@ -716,6 +728,8 @@ extension Layout { ...@@ -716,6 +728,8 @@ extension Layout {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -729,6 +743,8 @@ extension Layout { ...@@ -729,6 +743,8 @@ extension Layout {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -755,6 +771,8 @@ extension Layout { ...@@ -755,6 +771,8 @@ extension Layout {
public class func top(parent: UIView, child: UIView, top: CGFloat = 0) { public class func top(parent: UIView, child: UIView, top: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -767,6 +785,8 @@ extension Layout { ...@@ -767,6 +785,8 @@ extension Layout {
public class func left(parent: UIView, child: UIView, left: CGFloat = 0) { public class func left(parent: UIView, child: UIView, left: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -779,6 +799,8 @@ extension Layout { ...@@ -779,6 +799,8 @@ extension Layout {
public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0) { public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -791,6 +813,8 @@ extension Layout { ...@@ -791,6 +813,8 @@ extension Layout {
public class func right(parent: UIView, child: UIView, right: CGFloat = 0) { public class func right(parent: UIView, child: UIView, right: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -868,6 +892,8 @@ extension Layout { ...@@ -868,6 +892,8 @@ extension Layout {
public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) { public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
...@@ -880,6 +906,8 @@ extension Layout { ...@@ -880,6 +906,8 @@ extension Layout {
public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) { public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset))
child.setNeedsLayout()
child.layoutIfNeeded()
} }
/** /**
......
...@@ -30,40 +30,36 @@ ...@@ -30,40 +30,36 @@
import UIKit import UIKit
internal struct MaterialLayer { fileprivate struct MaterialLayer {
/// A reference to the CALayer. /// A reference to the CALayer.
internal weak var layer: CALayer? fileprivate weak var layer: CALayer?
/// A property that sets the height of the layer's frame. /// A property that sets the height of the layer's frame.
internal var heightPreset = HeightPreset.default { fileprivate var heightPreset = HeightPreset.default {
didSet { didSet {
layer?.height = CGFloat(heightPreset.rawValue) layer?.height = CGFloat(heightPreset.rawValue)
} }
} }
/// A property that sets the cornerRadius of the backing layer. /// A property that sets the cornerRadius of the backing layer.
internal var cornerRadiusPreset = CornerRadiusPreset.none { fileprivate var cornerRadiusPreset = CornerRadiusPreset.none {
didSet { didSet {
layer?.cornerRadius = CornerRadiusPresetToValue(preset: cornerRadiusPreset) layer?.cornerRadius = CornerRadiusPresetToValue(preset: cornerRadiusPreset)
} }
} }
/// A preset property to set the borderWidth. /// A preset property to set the borderWidth.
internal var borderWidthPreset = BorderWidthPreset.none { fileprivate var borderWidthPreset = BorderWidthPreset.none {
didSet { didSet {
layer?.borderWidth = BorderWidthPresetToValue(preset: borderWidthPreset) layer?.borderWidth = BorderWidthPresetToValue(preset: borderWidthPreset)
} }
} }
/// A preset property to set the shape. /// A preset property to set the shape.
internal var shapePreset = ShapePreset.none { fileprivate var shapePreset = ShapePreset.none
didSet {
layer?.layoutShape()
}
}
/// A preset value for Depth. /// A preset value for Depth.
internal var depthPreset: DepthPreset { fileprivate var depthPreset: DepthPreset {
get { get {
return depth.preset return depth.preset
} }
...@@ -73,7 +69,7 @@ internal struct MaterialLayer { ...@@ -73,7 +69,7 @@ internal struct MaterialLayer {
} }
/// Grid reference. /// Grid reference.
internal var depth = Depth.zero { fileprivate var depth = Depth.zero {
didSet { didSet {
guard let v = layer else { guard let v = layer else {
return return
...@@ -87,24 +83,24 @@ internal struct MaterialLayer { ...@@ -87,24 +83,24 @@ internal struct MaterialLayer {
} }
/// Enables automatic shadowPath sizing. /// Enables automatic shadowPath sizing.
internal var isShadowPathAutoSizing = false fileprivate var isShadowPathAutoSizing = false
/** /**
Initializer that takes in a CALayer. Initializer that takes in a CALayer.
- Parameter view: A CALayer reference. - Parameter view: A CALayer reference.
*/ */
internal init(layer: CALayer?) { fileprivate init(layer: CALayer?) {
self.layer = layer self.layer = layer
} }
} }
/// A memory reference to the MaterialLayer instance for CALayer extensions. /// A memory reference to the MaterialLayer instance for CALayer extensions.
private var MaterialLayerKey: UInt8 = 0 fileprivate var MaterialLayerKey: UInt8 = 0
/// Grid extension for UIView. /// Grid extension for UIView.
extension CALayer { extension CALayer {
/// MaterialLayer Reference. /// MaterialLayer Reference.
internal var materialLayer: MaterialLayer { fileprivate var materialLayer: MaterialLayer {
get { get {
return AssociatedObject(base: self, key: &MaterialLayerKey) { return AssociatedObject(base: self, key: &MaterialLayerKey) {
return MaterialLayer(layer: self) return MaterialLayer(layer: self)
...@@ -273,18 +269,24 @@ extension CALayer { ...@@ -273,18 +269,24 @@ extension CALayer {
/// Manages the layout for the shape of the view instance. /// Manages the layout for the shape of the view instance.
open func layoutShape() { open func layoutShape() {
if .none != shapePreset { guard .none != shapePreset else {
if frame.height > frame.width { return
}
if 0 == frame.width {
frame.size.width = frame.height frame.size.width = frame.height
} else if frame.height < frame.width { }
if 0 == frame.height {
frame.size.height = frame.width frame.size.height = frame.width
} }
guard .circle == shapePreset else {
return
} }
if .circle == shapePreset {
cornerRadius = bounds.size.width / 2 cornerRadius = bounds.size.width / 2
} }
}
/// Sets the shadow path. /// Sets the shadow path.
open func layoutShadowPath() { open func layoutShadowPath() {
......
...@@ -142,6 +142,7 @@ open class TabBar: Bar { ...@@ -142,6 +142,7 @@ open class TabBar: Bar {
prepareLineAnimationHandler(button: b) prepareLineAnimationHandler(button: b)
} }
} }
contentView.grid.axis.columns = buttons.count
contentView.grid.reload() contentView.grid.reload()
if nil == selected { if nil == selected {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment