11

これは単純なものである必要があります。基本的に、コア グラフィックスで描画されたパスがいくつかあり、それらを (便宜上) 回転できるようにしたいと考えています。CGContextRotateCTM(context); を使用してみました。しかし、それは何も回転していません。何か不足していますか?

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);
}
4

1 に答える 1

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 に渡された角度値を変更して、図形が別の方向を向くようにします。

于 2010-05-13T05:48:01.297 に答える