Commit 66bb1e7b by Daniel Dahan

development: improved performance when laying out ControlView controls

parent a6308b53
...@@ -33,7 +33,7 @@ import UIKit ...@@ -33,7 +33,7 @@ import UIKit
open class ControlView: View { open class ControlView: View {
/// Will render the view. /// Will render the view.
open var willRenderView: Bool { open var willRenderView: Bool {
return 0 < width && 0 < height return 0 < width && 0 < height && nil !== superview
} }
/// A preset wrapper around contentInset. /// A preset wrapper around contentInset.
...@@ -89,36 +89,28 @@ open class ControlView: View { ...@@ -89,36 +89,28 @@ open class ControlView: View {
open private(set) var contentView: View! open private(set) var contentView: View!
/// Left side UIControls. /// Left side UIControls.
open var leftControls: [UIView]? { open var leftControls: [UIView] {
didSet { didSet {
if let v = oldValue { for v in oldValue {
for b in v { v.removeFromSuperview()
b.removeFromSuperview()
}
} }
if let v = leftControls { for v in leftControls {
for b in v { addSubview(v)
addSubview(b)
}
} }
layoutSubviews() layoutSubviews()
} }
} }
/// Right side UIControls. /// Right side UIControls.
open var rightControls: [UIView]? { open var rightControls: [UIView] {
didSet { didSet {
if let v = oldValue { for v in oldValue {
for b in v { v.removeFromSuperview()
b.removeFromSuperview()
}
} }
if let v = rightControls { for v in rightControls {
for b in v { addSubview(v)
addSubview(b)
}
} }
layoutSubviews() layoutSubviews()
} }
...@@ -129,6 +121,8 @@ open class ControlView: View { ...@@ -129,6 +121,8 @@ open class ControlView: View {
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
leftControls = []
rightControls = []
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
...@@ -139,11 +133,15 @@ open class ControlView: View { ...@@ -139,11 +133,15 @@ open class ControlView: View {
- Parameter frame: A CGRect instance. - Parameter frame: A CGRect instance.
*/ */
public override init(frame: CGRect) { public override init(frame: CGRect) {
leftControls = []
rightControls = []
super.init(frame: frame) super.init(frame: frame)
} }
/// Basic initializer. /// Basic initializer.
public init() { public init() {
leftControls = []
rightControls = []
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
frame.size = intrinsicContentSize frame.size = intrinsicContentSize
} }
...@@ -154,9 +152,10 @@ open class ControlView: View { ...@@ -154,9 +152,10 @@ open class ControlView: View {
- Parameter rightControls: An Array of UIControls that go on the right side. - Parameter rightControls: An Array of UIControls that go on the right side.
*/ */
public init(leftControls: [UIView]? = nil, rightControls: [UIView]? = nil) { public init(leftControls: [UIView]? = nil, rightControls: [UIView]? = nil) {
self.leftControls = leftControls ?? []
self.rightControls = rightControls ?? []
super.init(frame: CGRect.zero) super.init(frame: CGRect.zero)
frame.size = intrinsicContentSize frame.size = intrinsicContentSize
prepareProperties(leftControls: leftControls, rightControls: rightControls)
} }
open override func layoutSubviews() { open override func layoutSubviews() {
...@@ -167,14 +166,13 @@ open class ControlView: View { ...@@ -167,14 +166,13 @@ open class ControlView: View {
let g = Int(width / gridFactor) let g = Int(width / gridFactor)
let columns = g + 1 let columns = g + 1
grid.views.removeAll() grid.views = []
grid.axis.columns = columns grid.axis.columns = columns
contentView.grid.columns = columns contentView.grid.columns = columns
// leftControls // leftControls
if let v = leftControls { for c in leftControls {
for c in v {
let w: CGFloat = c.intrinsicContentSize.width let w: CGFloat = c.intrinsicContentSize.width
(c as? UIButton)?.contentEdgeInsets = UIEdgeInsets.zero (c as? UIButton)?.contentEdgeInsets = UIEdgeInsets.zero
c.frame.size.height = frame.size.height - contentInset.top - contentInset.bottom c.frame.size.height = frame.size.height - contentInset.top - contentInset.bottom
...@@ -187,15 +185,13 @@ open class ControlView: View { ...@@ -187,15 +185,13 @@ open class ControlView: View {
addSubview(c) addSubview(c)
grid.views.append(c) grid.views.append(c)
} }
}
addSubview(contentView) addSubview(contentView)
grid.views.append(contentView) grid.views.append(contentView)
// rightControls // rightControls
if let v = rightControls { for c in rightControls {
for c in v { let w = c.intrinsicContentSize.width
let w: CGFloat = c.intrinsicContentSize.width
(c as? UIButton)?.contentEdgeInsets = UIEdgeInsets.zero (c as? UIButton)?.contentEdgeInsets = UIEdgeInsets.zero
c.frame.size.height = frame.size.height - contentInset.top - contentInset.bottom c.frame.size.height = frame.size.height - contentInset.top - contentInset.bottom
...@@ -207,11 +203,9 @@ open class ControlView: View { ...@@ -207,11 +203,9 @@ open class ControlView: View {
addSubview(c) addSubview(c)
grid.views.append(c) grid.views.append(c)
} }
}
grid.contentEdgeInsets = contentInset grid.contentEdgeInsets = contentInset
grid.interimSpace = interimSpace grid.interimSpace = interimSpace
grid.reload()
contentView.grid.reload() contentView.grid.reload()
} }
} }
...@@ -232,16 +226,6 @@ open class ControlView: View { ...@@ -232,16 +226,6 @@ open class ControlView: View {
prepareContentView() prepareContentView()
} }
/**
Used to trigger property changes that initializers avoid.
- Parameter leftControls: An Array of UIControls that go on the left side.
- Parameter rightControls: An Array of UIControls that go on the right side.
*/
internal func prepareProperties(leftControls: [UIView]?, rightControls: [UIView]?) {
self.leftControls = leftControls
self.rightControls = rightControls
}
/// Prepares the contentView. /// Prepares the contentView.
private func prepareContentView() { private func prepareContentView() {
contentView = View() contentView = View()
......
...@@ -199,8 +199,9 @@ public class Grid { ...@@ -199,8 +199,9 @@ public class Grid {
public func reload() { public func reload() {
var n: Int = 0 var n: Int = 0
print("before", views.count)
for i in 0..<views.count { for i in 0..<views.count {
print(i, views.count, views) print("after", i, views.count)
let child = views[i] let child = views[i]
if let parent = context { if let parent = context {
......
...@@ -70,7 +70,7 @@ public class NavigationBar: UINavigationBar { ...@@ -70,7 +70,7 @@ public class NavigationBar: UINavigationBar {
/// Will render the view. /// Will render the view.
public var willRenderView: Bool { public var willRenderView: Bool {
return 0 < width && 0 < height return 0 < width && 0 < height && nil != superview
} }
/// A preset wrapper around contentInset. /// A preset wrapper around contentInset.
......
...@@ -49,7 +49,7 @@ public class TabBar: View { ...@@ -49,7 +49,7 @@ public class TabBar: View {
/// Will render the view. /// Will render the view.
public var willRenderView: Bool { public var willRenderView: Bool {
return 0 < width return 0 < width && 0 < height && nil != superview
} }
/// Buttons. /// Buttons.
......
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