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