2017-05-30 31 views
0

我有程序創建這樣一個UIView:分割的圓形的UIView到5條相等角件

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) 

我需要添加(再次編程)5子視圖此UIView的。 我努力尋找subViews的錨點的座標。

思考成度,我圈出來的superView必須分成72°,每個相等的部分,邊界的座標必須是我的子視圖的錨點。

事情是這樣的: enter image description here

+1

見https://stackoverflow.com/questions/839899/how-do-i用於計算給定角度的圓上位置的圓周上的點。 – Gerriet

回答

2

像這樣(從頂部開始和去順時針):

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)") 
} 
+0

超級!我是一個三角學廢話:-D謝謝你! – Alex

+1

不客氣:-)如果不是很明顯:cx/cy是中心座標,您將使用-360/n而不是-72來表示圓上不同數量的位置。 – Gerriet

+0

一個愚蠢的問題:半徑這將是我的superView周長? – Alex