2010-04-06 62 views
11

这应该是一个简单的,基本上我有几个路径与核心图形绘制,我想能够旋转它们(为了方便)。我试过使用CGContextRotateCTM(上下文);但它没有旋转任何东西。我错过了什么吗?核心图形旋转路径

下面是这个拉丝/填充你的身材翻译原籍之前的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

你能否包含一些源代码? CGContextRotateCTM(上下文)是正确的功能,但如果您不包含任何代码,则很难看到代码中是否有错误! – 2010-04-06 15:46:38

+0

我更新了原始文章以包含源代码,任何帮助将不胜感激! – 2010-04-06 17:11:54

回答

23

广场,旋转,再转换回点应该出现左右旋转:

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

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

更改传递给CGContextRotateCTM的角度值,使图形指向另一个方向。

+0

这需要更多的爱,这对我很有用,正是我所需要的 – DFectuoso 2010-05-30 07:01:39

+0

只是永远花在这垃圾上。在几小时后登陆并且意识到我在2年前投票。这是一个非常简单的解决方案,完美无瑕。完全同意@DFectuoso :) – 2013-09-24 04:19:27

+0

什么是midX和midY – 2013-11-01 14:50:56