uiviewswift3coordinatesaddsubviewround-rect

Split a circular UIView into 5 equal angle pieces


I have a UIView created programmatically like that:

    forecastWeatherWheel.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.1)
    forecastWeatherWheel.frame = CGRect(x: -(dailyWeatherContainer.frame.width + 100)/2,
                                        y: self.view.bounds.height/2 - ((dailyWeatherContainer.frame.height + 100)/2),
                                        width: dailyWeatherContainer.frame.width + 100,
                                        height: dailyWeatherContainer.frame.height + 100)
    forecastWeatherWheel.layer.cornerRadius = forecastWeatherWheel.bounds.size.width/2
    forecastWeatherWheel.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(forecastWeatherWheel)

I need to add (again programmatically) 5 subViews to this UIView. I'm struggling in finding the coordinates for the anchor point of the subViews.

Thinking into Degrees, my circled superView will have to be split into 72° each equal pieces and the coordinates of the border will have to be the anchor point of my subViews.

Something like this: enter image description here


Solution

  • Like this (starting at the top and going clockwise):

    let radius: Double = 100
    let cx: Double = 0
    let cy: Double = 0
    for deg in stride(from: 90, to: -269, by: -72) {
        let a = Double(deg)*M_PI/180
        let x = cx + radius * cos(a)
        let y = cy + radius * sin(a)
        print("Angle \(a): \(x), \(y)")
    }