2010-04-06 16 views
11

Esto debería ser simple, básicamente tengo unas pocas rutas dibujadas con gráficos centrales, y quiero poder rotarlas (por conveniencia). He intentado usar CGContextRotateCTM (context); pero no está rotando nada. ¿Me estoy perdiendo de algo?Gráficos básicos girando una ruta

Aquí está la fuente de 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

¿Puede incluir algún código fuente? El CGContextRotateCTM (contexto) es la función correcta, pero es difícil ver si tiene un error en su código si no incluye ninguno. –

+0

Actualicé mi publicación original para incluir la fuente, ¡cualquier ayuda sería muy apreciada! –

Respuesta

23

Coloque este antes de dibujar/llenar su figura de traducir al origen, rotar y luego traducir de nuevo al punto que debería aparecer a girar en torno a:

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

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

Cambie el valor de ángulo pasado a CGContextRotateCTM para hacer que la figura apunte en otra dirección.

+0

Esto necesita más amor, esto funcionó para mí y es exactamente lo que necesitaba – DFectuoso

+0

Acabo de gastar para siempre en esta basura. Llegué aquí después de horas y me di cuenta que voté esto hace 2 años. Esta es una solución realmente simple que funciona sin problemas. Totalmente de acuerdo @DFectuoso :) –

+0

Qué es midX y midY –