Skip to content

Instantly share code, notes, and snippets.

@CrystDragon
Last active August 22, 2018 06:34
Show Gist options
  • Save CrystDragon/9167b4d13f80736220c19bc8a6897b4c to your computer and use it in GitHub Desktop.
Save CrystDragon/9167b4d13f80736220c19bc8a6897b4c to your computer and use it in GitHub Desktop.
Test relations between main properties of `UIView`
import UIKit
extension UIView {
func dumpProperties() {
print("""
- bounds: \(bounds)
- frame: \(frame)
- center: \(center)
- transform: \(transform)
""")
}
}
let superview = UIView()
let subview = UIView()
superview.addSubview(subview)
test(subview)
private func test(_ view: UIView) {
view.frame = CGRect(x: 10, y: 10, width: 20, height: 30)
view.bounds.origin = CGPoint(x: 5, y: 5)
view.dumpProperties()
// changing size from bounds will affect both frame(origin + size) and bounds(size), but not center
view.bounds.size = CGSize(width: 10, height: 10)
view.dumpProperties()
// changing size from frame will affect both frame(size) and bounds(size) and center, but not frame.origin
// note this difference because `CGRect` is a value object, this may seem unresonable to you
view.frame.size = CGSize(width: 20, height: 20)
view.dumpProperties()
// changing transform will affect frame(origin+size), but not center or bounds
view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
view.dumpProperties()
// changing frame/origin will not affect transform, so bounds.size may not be same with frame.size
view.frame = CGRect(x: 0, y: 0, width: 5, height: 5)
view.bounds.origin = CGPoint(x: 5, y: 5)
view.dumpProperties()
view.bounds.size = CGSize(width: 5, height: 5)
view.dumpProperties()
// changing center will only affect frame.origin
view.center = CGPoint(x: 0, y: 0)
view.dumpProperties()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment