2010-04-06 22 views
11

Dies sollte ein einfacher sein, im Grunde habe ich ein paar Pfade mit Kerngrafiken gezeichnet, und ich möchte sie drehen (für die Bequemlichkeit). Ich habe versucht, CGContextRotateCTM (Kontext) zu verwenden; aber es rotiert nichts. Fehle ich etwas?Core Graphics Rotieren eines Pfades

Hier ist die Quelle für drawRect

- (void)drawRect:(CGRect)rect { 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGContextSetLineWidth(context, 1.5); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetShadow(context, CGSizeMake(0, 1), 0); 

CGContextBeginPath(context); 

CGContextMoveToPoint(context, 13.5, 13.5); 
CGContextAddLineToPoint(context, 30.5, 13.5); 
CGContextAddLineToPoint(context, 30.5, 30.5); 
CGContextAddLineToPoint(context, 13.5, 30.5); 
CGContextAddLineToPoint(context, 13.5, 13.5); 

CGContextClosePath(context); 

CGContextMoveToPoint(context, 26.2, 13.5); 
CGContextAddLineToPoint(context, 26.2, 17.8); 
CGContextAddLineToPoint(context, 30.5, 17.8); 

CGContextMoveToPoint(context, 17.8, 13.5); 
CGContextAddLineToPoint(context, 17.8, 17.8); 
CGContextAddLineToPoint(context, 13.5, 17.8); 

CGContextMoveToPoint(context, 13.5, 26.2); 
CGContextAddLineToPoint(context, 17.8, 26.2); 
CGContextAddLineToPoint(context, 17.8, 30.5); 

CGContextStrokePath(context); 

CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 0, [UIColor clearColor].CGColor); 

CGContextFillRect(context, CGRectMake(26.2, 13.5, 4.3, 4.3)); 
CGContextFillRect(context, CGRectMake(13.5, 13.5, 4.3, 4.3)); 
CGContextFillRect(context, CGRectMake(13.5, 26.2, 4.3, 4.3)); 

CGContextRotateCTM(context, M_PI/4); 
} 
+0

Können Sie Quellcode hinzufügen? Der CGContextRotateCTM (Kontext) ist die korrekte Funktion, aber es ist schwierig zu sehen, ob Sie einen Fehler in Ihrem Code haben, wenn Sie keinen enthalten! –

+0

Ich aktualisierte meinen ursprünglichen Beitrag, um die Quelle einzuschließen, jede mögliche Hilfe würde sehr geschätzt werden! –

Antwort

23

Diese vor dem Zeichnen/Füllen Ihre Figur Herkunft zu übersetzen, zu drehen und dann auf den Punkt übersetzen zurück sollte es um zu drehen scheint:

static inline float radians(double degrees) { return degrees * M_PI/180; } 

CGContextTranslateCTM(c, midX, midY); 
CGContextRotateCTM(c, radians(-73)); 
CGContextTranslateCTM(c, -midX, -midY); 

Ändern Sie den an CGContextRotateCTM übergebenen Winkelwert, um den Zahlenpunkt in eine andere Richtung zu bringen.

+0

Das braucht mehr Liebe, das hat für mich funktioniert und ist genau das, was ich brauchte – DFectuoso

+0

Nur für immer auf diesem Müll verbracht. Habe hier nach Stunden gelandet und realisiert, dass ich das vor 2 Jahren gewählt habe. Dies ist eine wirklich einfache Lösung, die einwandfrei funktioniert. Stimmen Sie @DFectuoso völlig zu :) –

+0

Was ist midX und midY –

Verwandte Themen