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,38 +89,30 @@ open class ControlView: View { ...@@ -89,38 +89,30 @@ 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() }
}
} for v in rightControls {
addSubview(v)
if let v = rightControls { }
for b in v { layoutSubviews()
addSubview(b)
}
}
layoutSubviews()
} }
} }
...@@ -129,7 +121,9 @@ open class ControlView: View { ...@@ -129,7 +121,9 @@ 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) {
super.init(coder: aDecoder) leftControls = []
rightControls = []
super.init(coder: aDecoder)
} }
/** /**
...@@ -139,13 +133,17 @@ open class ControlView: View { ...@@ -139,13 +133,17 @@ 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) {
super.init(frame: CGRect.zero) self.leftControls = leftControls ?? []
self.rightControls = rightControls ?? []
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,51 +166,46 @@ open class ControlView: View { ...@@ -167,51 +166,46 @@ 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
let q: Int = Int(w / gridFactor)
let q: Int = Int(w / gridFactor) c.grid.columns = q + 1
c.grid.columns = q + 1
contentView.grid.columns -= c.grid.columns
contentView.grid.columns -= c.grid.columns
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
let q: Int = Int(w / gridFactor)
let q: Int = Int(w / gridFactor) c.grid.columns = q + 1
c.grid.columns = q + 1
contentView.grid.columns -= c.grid.columns
contentView.grid.columns -= c.grid.columns
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.
......
...@@ -101,39 +101,39 @@ public class Toolbar: BarView { ...@@ -101,39 +101,39 @@ public class Toolbar: BarView {
} }
/** /**
An initializer that initializes the object with a NSCoder object. An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance. - Parameter aDecoder: A NSCoder instance.
*/ */
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
/** /**
An initializer that initializes the object with a CGRect object. An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance If AutoLayout is used, it is better to initilize the instance
using the init() initializer. using the init() initializer.
- Parameter frame: A CGRect instance. - Parameter frame: A CGRect instance.
*/ */
public override init(frame: CGRect) { public override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
} }
/** /**
A convenience initializer with parameter settings. A convenience initializer with parameter settings.
- Parameter leftControls: An Array of UIControls that go on the left side. - Parameter leftControls: An Array of UIControls that go on the left side.
- 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 override init(leftControls: [UIView]? = nil, rightControls: [UIView]? = nil) { public override init(leftControls: [UIView]? = nil, rightControls: [UIView]? = nil) {
super.init(leftControls: leftControls, rightControls: rightControls) super.init(leftControls: leftControls, rightControls: rightControls)
} }
/** /**
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
to initialize property values and other setup operations. to initialize property values and other setup operations.
The super.prepareView method should always be called immediately The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
public override func prepareView() { public override func prepareView() {
super.prepareView() super.prepareView()
prepareTitleLabel() prepareTitleLabel()
......
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