简体   繁体   中英

Convert UIView center coordinates from local to window base coordinates?

I am trying to get the center points of all UITabBar subviews in the window base coordinate system using the following code.

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

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

The issue i have is the coordinates returned fall outside the device screen

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

What could I be doing wrong ?, Why don't return points fall within window device screen.

I believe you want to convert from the tabbar view ... so, try it like this:

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
    }

}

For a 3-item tab bar, I get this as a result (the first point is from the tabbar's background view):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM