试图创造一个财富之轮,困难创造轮子部分

时间:2015-10-06 14:41:57

标签: ios swift uibezierpath

我很快就会迅速发展。 我正试图为我的应用创造一个轮子财富,但我遇到了一些困难。我需要能够以不同的方式设置车轮的截面数量。有什么帮助怎么办? 我试着像这样创建椭圆:

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()

}

我认为不是这样做的,但我没有找到任何其他方法来做到这一点。

1 个答案:

答案 0 :(得分:0)

第一个示例的主要问题是您的上下文需要作为drawRect的一部分进行检索。一旦你有一个圆,创建n个段只是绘制n-1行的问题。

(线宽的插入会增加并发症,但看起来更好。)

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)
        }
    }

}