iOS坐标转换(func convert(_ point: CG
2017-01-16 本文已影响0人
xiAo__Ju
结论
// 把redView坐标系上的点greenViewOrigin转换为blueView的坐标系上的点为point
let point = redView.convert(greenViewOrigin, to: blueView)
// 把blueView坐标系上的点greenViewOrigin转换为redView的坐标系上的点为point1
let point1 = redView.convert(greenViewOrigin, from: blueView)
func convert(:to: ) -> CGPoint | func convert(: from:) -> CGPoint | |
---|---|---|
view为redView的子视图 | let result = point - view.frame.origin | let result = point + view.frame.origin |
view不为redView的子视图 | let result = point + redView.frame.origin - view.frame.origin | let result = point - redView.frame.origin + view.frame.origin |
// 把redView上的点greenViewOrigin相对blueView结果为
let point = redView.convert(greenViewOrigin, to: blueView)
print("point=\(point)")
// 把blueView上的点greenViewOrigin相对redView结果为
let point1 = redView.convert(greenViewOrigin, from: blueView)
print("point1=\(point1)")
let point2 = redView.convert(blueViewOrigin, to: greenView)
print("point2=\(point2)")
let point3 = redView.convert(blueViewOrigin, from: greenView)
print("point3=\(point3)")
print("\n\n\n\n\n")
let rect = redView.convert(greenView.frame, to: blueView)
print("rect=\(rect)")
let rect1 = redView.convert(greenView.frame, from: blueView)
print("rect1=\(rect1)")
输出
屏幕快照 2017-01-16 下午5.42.51.pngfunc convert(_ rect: CGRect, to view: UIView?) -> CGRect
同
func convert(_ point: CGPoint, to view: UIView?) -> CGPoint
原rect的宽高不变
func convert(_ rect: CGRect, from view: UIView?) -> CGRect
同
func convert(_ point: CGPoint, from view: UIView?) -> CGPoint
原rect的宽高不变
注意
以上方法中view为空时相对keywindow