<div dir="ltr"><span style="font-size:13px">One of the task that is performed often is center a frame with respect to it's parent.</span><div style="font-size:13px">The code for this is short and simple:</div><div style="font-size:13px"><br></div><div style="font-size:13px">rect.origin.x = (rect.width-parent.width)/2 // or... </div><div style="font-size:13px">rect.origin.y = (rect.height-parent.height)/2</div><div style="font-size:13px"><br></div><div style="font-size:13px">## Current Problems</div><div style="font-size:13px"><br></div><div style="font-size:13px">- It is very easy to get it wrong and confuse X or Y and their length associations.</div><div style="font-size:13px">- Because this code is often found in a layout method, several other rect variables may be defined in the scope which makes it easier to mistake one variable with another if their names are close (and you are used to autocomplete)</div><div style="font-size:13px">- And finally but most importantly, while the equation here is simple, one must parse it and understand it and is not a very swifty approach.</div><div style="font-size:13px"><br></div><div style="font-size:13px">##Proposed Additions </div><div style="font-size:13px"><br></div><div style="font-size:13px">2 (actually 4) extensions in CGGeometry to CGRect:</div><div style="font-size:13px"><br></div><div style="font-size:13px">extension CGRect {</div><div style="font-size:13px"> public func centerX(parentRect: CGRect) -> CGRect</div><div style="font-size:13px"> public mutating func centerXInPlace(parentRect: CGRect)</div><div style="font-size:13px"><div> public fun centerY(parentRect: CGRect) -> CGRect</div><div> public mutating func centerYInPlace(parentRect: CGRect)</div></div><div style="font-size:13px">}</div><div style="font-size:13px"><br></div><div style="font-size:13px">This extension allows very easily (and verbally) center a rect in respect to a parent.</div><div style="font-size:13px"><br></div><div style="font-size:13px">I'm pretty sure there are other pretty useful extensions that can be added to CGGeometry as well but this one I think is basic and pretty important.</div><div><br></div>-- <br><div class="gmail_signature"><div style="color:rgb(34,34,34)">++++++++++++++++++++++++++</div><div style="color:rgb(34,34,34)">Diego Torres.</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">Web: <a href="http://dtorres.me/" target="_blank">dtorres.me</a></div></div>
</div>