我想在我当前的 CGGraphicsContext
中绘制一个 UIView
。我通过 renderInContext:
绘制了 UIView
,但它的位置不正确(总是在左上角)。
我拥有可用于绘制 UIView
的 UIView
的所有值。
CGRect frame;
CGRect bound;
CGPoint center;
CGAffineTransform transform;
我目前这样设置图层的位置和旋转:
CGContextTranslateCTM(context, frame.origin.x, frame.origin.y);
CGContextRotateCTM(context, (CGFloat) atan2(transform.b, transform.a));
但由于旋转,位置不正确,与原始位置不同。
我怎样才能恢复原来的位置?坐标系在两者之间没有改变,只是将 center.x
和 center.y
值转换为适合 CGContextTranslateCTM
的值的问题>.
编辑:
保存的值是正确的,保存正确的bounds
,center
和transform
不是我的问题的根源,我的问题的根源是通过 CGContextTranslateCTM
将这些值设置为可绘制的 CGLayer
。
最佳答案
可以使用以下代码片段将图形上下文的几何形状调整为 UIView
的几何形状:
// 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);
关于ios - 通过 renderInContext : 定位用于绘制的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23110601/