quartz2d 翻译起源
我对quartz2d的理解是代码CGContextTranslateCTM(context, x, y);
平移坐标系。我有一个quartz2d 视图,上面有很多形状,用户需要能够平移和缩放它。但是,当使用 CGContextScaleCTM(context,scaleX,scaleY); 代码时,所有内容都会围绕原点缩放,而不是用户正在查看的视点中心。
我的解决方案是使用以下代码:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 512.0+offset.x, 384.0+offset.y);
//(512, 384) is the center of the iPad screen
CGContextScaleCTM(context, scale, scale);
您可以很好地平移,但事情仍然会扩展到角落。怎么了?
编辑:哦。哇。呃。如果移动原点,形状也会移动,因此您无法相对于形状移动它。现在我知道出了什么问题,但我该怎么做?(独立于形状移动原点)
My understanding of quartz2d is that the code CGContextTranslateCTM(context, x, y);
translates the coordinate system. I have a quartz2d view with lots of shapes on it, and the user needs to be able to pan around and zoom it. However, when using the CGContextScaleCTM(context, scaleX, scaleY);
code, everything scales around the origin, not the center of the viewpoint the user is viewing.
My solution to this was to use the following code:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 512.0+offset.x, 384.0+offset.y);
//(512, 384) is the center of the iPad screen
CGContextScaleCTM(context, scale, scale);
You can translate around fine, but things still scale into the corner. What's wrong?
EDIT: Oh. Wow. Duh. If you move the origin, the shapes move too, so you can't move it relative to the shapes. Now I know what's wrong, but how do I do that?(move the origin independently of the shapes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有太多使用 CoreGraphics C API 的经验,但您是否考虑过将视图粘贴到 UISCrollView 上?如果该功能存在并且免费,为什么不使用它呢?
Don't have much experience with using the CoreGraphics C API, but have you considered sticking your view onto a UISCrollView? If the functionality is there and its free, why not use it?
您可以为每个形状创建一个 UIView 类。
为每个要移动的形状实例化一个 UIView 对象。
然后使用 shape.center = CGPointMake(x,y); 更改视图对象的中心。
大概的方法是调用bounds.center,在UIView中查看。
这将自动调用drawRect(),一切都很好!
You could create a UIView class for each shape.
The instantiate an UIView object for each shape to move arround.
Then you change the center of tht view objects with shape.center = CGPointMake(x,y);
Probably the method is calls bounds.center, look in UIView.
This will automatically call drawRect() and all is fine!