Position a View for drawing via renderInContext:

我与影子孤独终老i 提交于 2019-12-04 15:32:05

问题


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).

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

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.

Edit:
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.


回答1:


The geometry of the graphics context can be adjusted to the UIViews 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);


来源:https://stackoverflow.com/questions/23110601/position-a-view-for-drawing-via-renderincontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!