通过renderInContext放置一个视图以进行绘制: [英] Position a View for drawing via renderInContext:
问题描述
我想在当前的 CGGraphicsContext
中绘制一个 UIView
。我通过 renderInContext:
绘制了 UIView
,但是它的位置不正确(总是在左上角)。
I want to draw a UIView
in my current CGGraphicsContext
. I draw the UIView
via renderInContext:
, but it's not positioned correctly (always in the top left corner).
我具有 UIView
的所有值,可用于绘制 UIView
。
I have all values of the UIView
available for drawing the UIView
.
CGRect frame;
CGRect bound;
CGPoint center;
CGAffineTransform transform;
我目前这样设置图层的位置和旋转:
I currently set the position and rotation of the layer like this:
CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
CGContextRotateCTM(context, (CGFloat) atan2(transform.b, transform.a));
但是该位置由于轮换而不正确,并且与原始位置不同。
如何恢复原始位置?坐标系统在这两者之间没有改变,只是转换 center.x
和 center.y
的问题值转换为适合 CGContextTranslateCTM
的值。
But the position is, due to the rotation, not correct and differs from the original position.
How can I restore the original position? The coordinate system hasn't changed in between, it's just an issue to translate the center.x
and center.y
values into something appropriate for CGContextTranslateCTM
.
编辑:
保存的值正确,保存了正确的边界
,中心
和变换
不是我的问题的起源,而是我的问题的起源,正在通过 CGContextTranslateCTM将这些值设置为可绘制的
。 CGLayer
The saved values are correct, saving the correct bounds
, center
and transform
is not the origin of my issue, the origin of my issue, is setting theses values to the drawable CGLayer
via CGContextTranslateCTM
.
推荐答案
图形上下文的几何可以调整为 UIView
具有以下代码段的几何:
The geometry of the graphics context can be adjusted to the UIView
s geometry with this snippet:
// Center the context around the view's anchor point
CGContextTranslateCTM(context, [view center].x, [view center].y);
// Apply the view's transform about the anchor point
CGContextConcatCTM(context, [view transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[view bounds].size.width * [[view layer] anchorPoint].x,
-[view bounds].size.height * [[view layer] anchorPoint].y);
这篇关于通过renderInContext放置一个视图以进行绘制:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!