Sto cercando di adattare un esempio fornito da Apple per attirare programmazione stelle, in linea, il codice è il seguente:Come disegnare stelle usando Quartz Core?
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, aSize);
for (NSUInteger i=0; i<stars; i++)
{
CGContextSetFillColorWithColor(context, aColor);
CGContextSetStrokeColorWithColor(context, aColor);
float w = item.size.width;
double r = w/2;
double theta = 2 * M_PI * (2.0/5.0); // 144 degrees
CGContextMoveToPoint(context, 0, r);
for (NSUInteger k=1; k<5; k++)
{
float x = r * sin(k * theta);
float y = r * cos(k * theta);
CGContextAddLineToPoint(context, x, y);
}
CGContextClosePath(context);
CGContextFillPath(context);
}
Il codice di cui sopra disegna un perfetto stella, ma è 1. visualizzato a testa in giù 2. è nero e senza bordo Quello che voglio fare è disegnare molte stelle sulla stessa linea e con lo stile dato. Capisco che in realtà sto disegnando lo stesso percorso 5 volte nella stessa posizione e che ho in qualche modo capovolto verticalmente il contesto, ma dopo diversi test ho rinunciato! (Mi mancano le competenze matematiche e geometriche necessarie: P) ... potresti aiutarmi per favore?
UPDATE:
Ok, grazie ad CocoaFu, questo è il mio refactoring e di lavoro di utilità pareggio:
- (void)drawStars:(NSUInteger)count inContext:(CGContextRef)context;
{
// constants
const float w = self.itemSize.width;
const float r = w/2;
const double theta = 2 * M_PI * (2.0/5.0);
const float flip = -1.0f; // flip vertically (default star representation)
// drawing center for the star
float xCenter = r;
for (NSUInteger i=0; i<count; i++)
{
// get star style based on the index
CGContextSetFillColorWithColor(context, [self fillColorForItemAtIndex:i]);
CGContextSetStrokeColorWithColor(context, [self strokeColorForItemAtIndex:i]);
// update position
CGContextMoveToPoint(context, xCenter, r * flip + r);
// draw the necessary star lines
for (NSUInteger k=1; k<5; k++)
{
float x = r * sin(k * theta);
float y = r * cos(k * theta);
CGContextAddLineToPoint(context, x + xCenter, y * flip + r);
}
// update horizontal center for the next star
xCenter += w + self.itemMargin;
// draw current star
CGContextClosePath(context);
CGContextFillPath(context);
CGContextStrokePath(context);
}
}
Cosa vuoi dire a testa in giù? Una stella non ha un "fondo" in quanto tale, sbaglio? Forse potresti pubblicare un paio di schermate, di quello che stai ricevendo e di quello che ti aspetti. –
Una stella ha solitamente un punto in alto e un 2 in basso. Stella normale: http://www.allstarbaseballcamp.com/star_clipart.gif, stella capovolta (verticale capovolta): http://sunandshield.files.wordpress.com/2010/03/eastern-star.jpg – daveoncode
Vedere https: //calayer.com/core-animation/2016/05/22/cashapelayer-in-depth.html#path – iwasrobbed