2015-12-12 34 views
1

我有一個按鈕陣列,當我將它們追加到一個視圖中時,我希望將它定位在位於中心的圖像視圖周圍。根據陣列中有多少物體,我希望它們在整個圓周上均勻分佈。以下是我的嘗試。我做錯了什麼,我該如何解決?駝鹿後面有不止一個按鈕。如何將對象放置在中心圖像周圍的陣列中?

enter image description here

var userbutton = [UIButton]() 
var upimage = [UIImage]() 
var locationpic = [AnyObject]() 

func locationsSet(){ 

    for (index, users) in upimage.enumerate() { 

     let userbutton = UIButton() 
     userbutton.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside) 
     userbutton.frame = CGRectMake(100, 100, 50, 50) 
     userbutton.layer.cornerRadius = userbutton.frame.size.width/2 
     userbutton.clipsToBounds = true 
     userbutton.setImage(users, forState: .Normal) 

     let radians = CGFloat(M_PI) * 2.0/CGFloat(upimage.count) * CGFloat(index) 

     let centerx = self.view.bounds.width/2.0 
     let radius = currentuserpic.frame.size.width/2.0 
     let centery = self.view.bounds.height/2.0 

     let pointx = centerx + cos(radians) * (radius + 40) 
     let pointy = (centery) + (sin(radians)) * (radius + 40) 

     userbutton.center.x = pointx 
     userbutton.center.y = pointy 

     self.userbutton.append(userbutton) 
     self.view.addSubview(userbutton) 

     print("x\(pointx)") 
     print("y\(pointy)") 


    } 


} 
+0

這是不是從一個教程。 – stackerleet

+0

這不是作業。這是我自己的個人應用程序。 – stackerleet

+0

我被卡住了我不是要求你寫代碼,我只是想弄清楚如何根據數組中的對象數量將數組中的按鈕定位在某個位置。 – stackerleet

回答

2

我將如何做到這一點:

創建一個擴展的UIView,以獲得對角和半徑。這些都很方便,因爲我們希望我們的「衛星」即使在「行星」不是方形時也具有可預測的放置。

extension UIView { 

    var diagonal : CGFloat { 
     return sqrt(pow(self.frame.width, 2) + pow(self.frame.height, 2)) 
    } 

    var radius : CGFloat { 
     return diagonal/2 
    } 
} 

這將返回基於角和從原點的距離的點。 它使用可怕的三角。

func getPoint(fromPoint point: CGPoint, atDistance distance: CGFloat, withAngleRadians angle:CGFloat) -> CGPoint { 

    let x = point.x 
    let y = point.y 

    let dx = (distance * cos(angle)) 
    let dy = (distance * sin(angle)) 

    return CGPoint(x: (dx + x), y: (dy + y)) 
} 

現在真正的功能。在圓形圖案中生成一堆點。我使用角度的運行總和而不是每次乘以索引。這只是返回視圖的中心點。現在

func encirclePoint(point : CGPoint, distance:CGFloat, inParts parts: Int) -> [CGPoint] { 

    let angle = 2 * CGFloat(M_PI)/CGFloat(parts) // critical part, you need radians for trigonometry 
    var runningAngle : CGFloat = -(CGFloat(M_PI)/2) // start at the top 

    var points : [CGPoint] = [] 

    for _ in 0..<parts { 
     let circlePoint = getPoint(fromPoint: point, atDistance: distance, withAngleRadians: runningAngle) 
     points.append(circlePoint) 
     runningAngle += angle 
    } 

    return points 
} 

,你可以創建一個簡單的函數,它接受一個觀點,保證金和「衛星」的意見的數組。它會設置它們的中心並將它們添加到我們以前輸入的視圖的超級視圖中。不要將它們添加到視圖本身是有意義的,因爲它們可能不會放在視圖中。

func encircleView(view : UIView, withSubViews subViews : [UIView], withMargin margin : CGFloat) { 

    guard !(subViews.isEmpty) else { // if there are no subviews : abort 
     return 
    } 

    let distance = view.radius + margin 
    let points = encirclePoint(view.center, distance: distance, inParts: subViews.count) 

    guard subViews.count == points.count, let uberView = view.superview else { // if the count is not the same or there is no superview: abort 
     return 
    } 

    for (point, subView) in zip(points, subViews) { subView.center = point } 
} 

通知我怎麼什麼也沒做,除了在這些功能的中心計算。造型他們在另一個功能。這使得它很容易維護和調試。

我甚至可能會讓最後一個函數返回帶有更新幀的子視圖,並在稍後添加它們。


enter image description here


或負利潤率:)

enter image description here

Gist

2

一個整圓,2個* PI弧度。您需要將其除以您擁有的物品數量,然後將其乘以當前正在處理的物品的索引。使用制動,以圓上找到位置:

for (index, users) in upimage.enumerate() { 
    let radians = CGFloat(M_PI) * 2.0/CGFloat(upimage.count) * CGFloat(index) 

    ...... 

    let centerx = self.view.bounds.width/2.0 
    let radius = currentuserpic.frame.size.width/2.0 
    let centery = self.view.bounds.height/2.0 

    let pointx = centerx + cos(radians) * radius 
    let pointy = centery + sin(radians) * radius 

    ...... 
} 
+0

我得到一個錯誤http://puu.sh/lTnw4/cf8f60289b.png – stackerleet

+1

我在'radians'的計算中拋出了一些'CGFloat',包括@RMenke建議的一個(謝謝!)。看一看。 – vacawama

+0

爲什麼它看起來像這樣? http://puu.sh/lTA5Y/da2ce9d364.png – stackerleet

相關問題