Post

convert(_:to:) / convert(_:from:)

convert(_:to:) / convert(_:from:)

1. convert(_:to:)

1
2
3
let newRect = myView.convert(myView.bounds, to: myView.superView)

//이 말은 myView.bounds가 myView.superView 기준으로의 상대적 좌표값을 반환한다는 뜻

2. convert(_:from:)

1
2
let newRect = myView.convert(viewB.frame, from: viewA)
//viewA 기준으로의 viewB,frame을 myView기준으로의 좌표값을 반환

- UIWindow 기준으로 뷰 위치 찾기

상황

  • 특정 뷰가 UIViewController 안에 있는데
  • 이 뷰의 위치를 UIWindow 기준으로 변환하여, 화면 전체에서 정확한 위치를 알고 싶음

예제 코드

1
2
3
4
if let window = UIApplication.shared.windows.first {
    let rectInWindow = window.convert(myView.frame, from: myView.superview)
    print("뷰의 위치 (윈도우 기준): \(rectInWindow)")
}

작동 원리

  • myView.framesuperview 기준
  • window.convert(_:from:)을 사용하여 화면 전체(UIWindow) 기준으로 변환
  • 팝업, 애니메이션, 드래그 이벤트 등에서 유용하게 활용 가능
This post is licensed under CC BY 4.0 by the author.