2011-05-02 9 views
11

necesito para dibujar la siguiente imagen enter image description hereDibujo hueco en iPhone

La parte gris es lo que quiero dibujar sobre otra imagen lo es el código que necesito utilizar el uso de métodos CGContext, He intentado utilizar el CGContextAddArc pero Falló porque cuando llené el trazo, el hueco central también se rellenó con la textura gris.

Cualquier ayuda apreciada.

Info: Tengo el completar la imagen azul, tengo que añadir el Semicírculo por encima de la imagen azul

Gracias

+0

¿Cómo hacer un trabajo en torno al intentar crear una lástima rosquilla utilizando cgcontext y luego añadir esto como a una capa o shapelayer ... Lo siento por esto, pero no estoy recibiendo mi cabeza alrededor de esto en este momento, por lo que en el momento de disparar en blanco – tosi

Respuesta

10

de trabajo más de lo Ole Begemann se refirió en su respuesta y algunas modificaciones, que era capaz de lograr el necesario.

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor); 
CGMutablePathRef path = CGPathCreateMutable(); 
CGContextSetLineWidth(context, 40); 
CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0); 
CGContextAddPath(context, path); 
CGContextStrokePath(context); 
CGPathRelease(path); 

Así que en lugar de 2 arcos utilicé solo uno y lo acaricié con un ancho mayor.

Espero que esto ayude a otros que tengan el mismo requisito.

+2

Quizás use constante M_PI en lugar de 3.142 ... – RobP

24

un vistazo a Filling a Path en la documentación núcleo de gráficos. Básicamente, lo que debes hacer es agregar dos arcos a tu ruta (la externa y la interna) y luego usar reglas de relleno de gráficos centrales para tu ventaja. El código sería algo como esto:

CGMutablePathRef path = CGPathCreateMutable(); 

// Add the outer arc to the path (as if you wanted to fill the entire circle) 
CGPathMoveToPoint(path, ...); 
CGPathAddArc(path, ...); 
CGPathCloseSubpath(path); 

// Add the inner arc to the path (later used to substract the inner area) 
CGPathMoveToPoint(path, ...); 
CGPathAddArc(path, ...); 
CGPathCloseSubpath(path); 

// Add the path to the context 
CGContextAddPath(context, path); 

// Fill the path using the even-odd fill rule 
CGContextEOFillPath(context); 

CGPathRelease(path); 
+0

Muchas gracias intentaré esto y actualizar :) – RVN

4

Esto es tangencial pero algo relevante. Aquí está mi código de dibujo de Swift para una rosquilla (o círculo hueco).

class func drawDonut(donutColor: UIColor, rect: CGRect, innerDonutProportion: CGFloat) { 

    //// Variable Declarations 
    let innerDonutSide: CGFloat = rect.size.width * innerDonutProportion 
    let innerDonutOffset: CGFloat = 0.5 * (rect.size.width - innerDonutSide) 
    let innerDonutRect = CGRectMake(innerDonutOffset, innerDonutOffset, innerDonutSide, innerDonutSide) 

    //// InnerCircle Drawing 
    let innerCirclePath = UIBezierPath(ovalInRect: innerDonutRect) 

    //// Outer Circle Drawing 
    let outerCirclePath = UIBezierPath(ovalInRect: rect) 

    // Clip out the innerCirclePath 
    outerCirclePath.appendPath(innerCirclePath) 
    outerCirclePath.addClip() 
    outerCirclePath.usesEvenOddFillRule = true; 

    // and Fill the outerCircle 
    donutColor.setFill() 
    outerCirclePath.fill() 
} 
+0

¡Agradable! Pero para el caso general en el que rect.origin no es (0,0), debe usar: let innerDonutRect = CGRectMake (rect.origin.x + innerDonutOffset, rect.origin.y + innerDonutOffset, innerDonutSide, innerDonutSide). Excepto por eso, tu código funcionó perfectamente para mí. – rene