iOS 中不同 view 的 frame 所處座標系可能不同,因此常需要將矩形或點從一個 view 轉換到另一個 view。最常用的是 convertRect:toView: 與 convertRect:fromView:。
例如要把 table view 中某個 cell 的 frame 轉成相對於 self.view 的座標:
CGRect convertedRect = [tableView convertRect:cell.frame toView:self.view];
若 view 參數為 nil,通常會以 window 的座標系為基準。另有 convertPoint:toView: 與 convertPoint:fromView:,用法類似,只是處理 CGPoint。
使用時要特別注意方向:toView 是「從目前 view 轉到目標 view」,fromView 是「從來源 view 轉回目前 view」,寫反容易得到錯誤位置。