2015-10-06 43 views
0

我是新來的迅速和發展。 我正在嘗試爲我的應用創建一個輪子財富,但我遇到了一些困難。我需要能夠每次不同地設置輪子的部分數量。任何幫助如何做到這一點? 我試圖創建橢圓這樣的:試圖創造財富之輪,困難創造車輪部分

import UIKit 

class addShape: UIView { 

    let context = UIGraphicsGetCurrentContext(); 

    override func drawRect(rect: CGRect) { 

     CGContextSetLineWidth(context, 3.0) 
     //CGContextSetStrokeColorWithColor(context, UIColor.purpleColor().CGColor) 

     CGContextStrokeEllipseInRect(context, rect) 
     UIColor.redColor().set() 

     //Actually draw the path 
     CGContextStrokePath(context) 
    } 
} 

,但我不能在所有:(

創建橢圓我想這樣也:

override func drawRect(rect: CGRect) { 
    ////UTUBE 
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 512, height: 512), false, 0) 


    // Define the center point of the view where you’ll rotate the arc around 
    let center = CGPoint(x:bounds.width/2, y: bounds.height/2) 

    // Calculate the radius based on the max dimension of the view. 
    let radius: CGFloat = max(bounds.width, bounds.height) 

    // Define the thickness of the arc. 
    let arcWidth: CGFloat = 115 


    /////FIRST Shape 
    // Define the start and end angles for the arc. 
    let startAngle: CGFloat = 2 * π 
    let endAngle: CGFloat = π/4 

    // Create a path based on the center point, radius, and angles you just defined. 
    var path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle, 
     endAngle: endAngle, 
     clockwise: true) 

    // Set the line width and color before finally stroking the path. 
    path.lineWidth = arcWidth 
    counterColor.setStroke() 
    path.stroke() 


    // Second Shape 
    let startAngle2: CGFloat = π/4 
    let endAngle2: CGFloat = π/2 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle2, 
     endAngle: endAngle2, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.greenColor().setStroke() 
    path.stroke() 


    // 3rd Shape 
    let startAngle3: CGFloat = π/2 
    let endAngle3: CGFloat = 3 * π/4 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle3, 
     endAngle: endAngle3, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.blueColor().setStroke() 
    path.stroke() 

    // 4th Shape 
    let startAngle4: CGFloat = 3 * π/4 
    let endAngle4: CGFloat = π 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle4, 
     endAngle: endAngle4, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.redColor().setStroke() 
    path.stroke() 

    // 5th Shape 
    let startAngle5: CGFloat = π 
    let endAngle5: CGFloat = 5 * π/4 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle5, 
     endAngle: endAngle5, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.yellowColor().setStroke() 
    path.stroke() 

    // 6th Shape 
    let startAngle6: CGFloat = 5 * π/4 
    let endAngle6: CGFloat = 3 * π/2 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle6, 
     endAngle: endAngle6, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.grayColor().setStroke() 
    path.stroke() 

    // 7th Shape 
    let startAngle7: CGFloat = 3 * π/2 
    let endAngle7: CGFloat = 7 * π/4 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle7, 
     endAngle: endAngle7, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.purpleColor().setStroke() 
    path.stroke() 

    // 8th Shape 
    let startAngle8: CGFloat = 7 * π/4 
    let endAngle8: CGFloat = 2 * π 

    // Create a path based on the center point, radius, and angles you just defined. 
    path = UIBezierPath(arcCenter: center, 
     radius: radius/2 - arcWidth/2, 
     startAngle: startAngle8, 
     endAngle: endAngle8, 
     clockwise: true) 

    path.lineWidth = arcWidth 
    UIColor.lightGrayColor().setStroke() 
    path.stroke() 

} 

我不認爲這是做到這一點的方式,但我沒有發現任何其他方式來做到這一點。

+0

請格式化(使用正確的縮進,不要增加不必要的空行)代碼正確發佈時,它可以更容易爲其他人瞭解。 –

+0

使用π因爲你可以自我挫敗。 「M_PI」,「M_PI_2」和「M_PI_4」定義已經可用,並且更具可讀性。 – Abizern

+0

有沒有辦法創建具有動態數字的橢圓? – Peter

回答

0

與你的第一個例子中的主要問題是,你的情況下需要檢索爲的一部分。一旦你有一個圓圈,創建n段只是一個繪製n-1行的問題。

(Insetting線路寬度增加了複雜性,但看起來更好。)

class WheelView: UIView { 

    var pieSections: Int = 1 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func drawRect(rect: CGRect) { 
     let context = UIGraphicsGetCurrentContext(); 
     let lineWidth: CGFloat = 3.0 

     // Draw outline 
     CGContextSetLineWidth(context, lineWidth) 
     UIColor.redColor().set() 
     let smaller = rect.insetBy(dx: lineWidth/2.0, dy: lineWidth/2.0) 
     CGContextStrokeEllipseInRect(context, smaller) 

     let radius = smaller.width/2.0 
     let segmentAngle = 2.0/CGFloat(pieSections) * CGFloat(M_PI) 

     // Draw segment dividers 
     for index in 1...pieSections { 
      let drawAngle = segmentAngle * CGFloat(index) 
      let x = radius * cos(drawAngle) + rect.width/2.0 
      let y = radius * sin(drawAngle) + rect.height/2.0 
      CGContextBeginPath(context) 
      CGContextMoveToPoint(context, rect.width/2, rect.height/2) 
      CGContextAddLineToPoint(context, x, y) 
      CGContextStrokePath(context) 
     } 
    } 

} 
+0

但我該如何切片?我需要將它切成相等的部分 – Peter

+0

@Peter - 更新後顯示一個繪圖示例。 –