gpt4 book ai didi

ios - 将 UIView 中心坐标从本地坐标转换为窗口基准坐标?

转载 作者:行者123 更新时间:2023-11-28 10:37:09 26 4
gpt4 key购买 nike

我正在尝试使用以下代码获取窗口基坐标系中所有 UITabBar subview 的中心点。

func getHighlightLocations() -> [CGPoint] {
var cgpointArray = [CGPoint]()

self.tabBar.subviews.foreach {
let point = $0.convert($0.center, to: nil)
cgpointArray.append(point)
}
return cgpointArray
}

我遇到的问题是返回的坐标落在设备屏幕之外

po cgpointArray
▿ <Array<CGPoint>>
▿ some : 6 elements
▿ 0 : (252.0, 123.3)
- x : 252.0
- y : 123.3
▿ 1 : (207.0, 844.0)
- x : 207.0
- y : 844.0
▿ 2 : (54.0, 828.5)
- x : 54.0
- y : 828.5
▿ 3 : (261.5, 828.5)
- x : 261.5
- y : 828.5
▿ 4 : (675.5, 828.5)
- x : 675.5
- y : 828.5
▿ 5 : (468.0, 828.5)
- x : 468.0
- y : 828.5

我可能做错了什么?,为什么返回点不落在窗口设备屏幕内。

最佳答案

我相信你想从标签栏 View 转换 ...所以,试试这样:

class MyTabBarController: UITabBarController {

func getHighlightLocations() -> [CGPoint] {
var cgpointArray = [CGPoint]()

tabBar.subviews.forEach {
let point = tabBar.convert($0.center, to: nil)
cgpointArray.append(point)
}

return cgpointArray
}

}

对于 3 项选项卡栏,我得到了这个结果(第一点来自选项卡栏的背景 View ):

[(187.5, 642.5), (62.5, 643.0), (187.5, 643.0), (312.5, 643.0)]

关于ios - 将 UIView 中心坐标从本地坐标转换为窗口基准坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52954354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com