Commit 66bb1e7b by Daniel Dahan

development: improved performance when laying out ControlView controls

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