2010-04-06 61 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