iphone - 核心显卡旋转路径

标签 iphone cgcontext core-graphics

这应该是一个简单的,基本上我有一些用核心图形绘制的路径,并且我希望能够旋转它们(为了方便)。我尝试过使用 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);
}

最佳答案

在绘制/填充图形之前放置它以平移到原点,旋转,然后平移回它应该旋转的点:

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

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

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

关于iphone - 核心显卡旋转路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2586069/

相关文章:

iphone - 获取 CGContext 绘制的 Arc 中的 Touch point

ios - iOS:使用CGContextRotate旋转

ios - 按 subview 剪切父 View ,并将第一个 View 的内容放入第二个 View 中,但进行缩放

cocoa-touch - 绘制圆并存储在 UIImage 中

iphone - CTFramesetterSuggestFrameSizeWithConstraints 有时返回不正确的大小?

iphone - 从 UIImagePicker 中删除相机控件 - 如何放大到全屏 - iPhone/iPad

没有 iPhone 6 Plus 的 iOS 应用程序预览

iphone - self.navigationController == 无

iphone - 屏幕上的 CGRect 网格?

ios - 如何在 Swift 中使用 CG 类创建下面附加的 UI?