【问题标题】:Change the coordinate system in UIKit and/or CoreGraphics更改 UIKit 和/或 CoreGraphics 中的坐标系
【发布时间】:2014-02-21 01:11:46
【问题描述】:

是否可以在 UIKit 或 CoreGraphics 框架中更改坐标系的原点?我附上了一张图片来解释我的意思:

谢谢!

【问题讨论】:

  • 我尝试了变换,但子视图也会旋转,我不想要它。如果可能的话,我只需要旋转坐标系

标签: ios uikit core-graphics coordinates


【解决方案1】:

当你设置一个视图的transform时,它有两个效果:

  1. 视图将变换应用于drawRect: 中使用的图形上下文。
  2. 视图将变换应用于用于布置其子视图的坐标系。

从您的评论看来,您想要#1 而不是#2。以下是一些无需 #2 即可获得 #1 的方法:

  1. 根本不要设置视图的transform。相反,将转换应用于 drawRect: 中的图形上下文,如下所示:

    CGAffineTransform transform = ...
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), transform);
    

    或使用CGContextRotateCTM 等方法直接在图形上下文中构建变换。

  2. 为绘图创建一个单独的子视图(具有自己的UIView 子类),并在该子视图上设置变换。因此,您的视图层次结构将如下所示:

    view
        subview for drawing, with transform
        other, untransformed subview #1
        other, untransformed subview #2
        other, untransformed subview #3
        etc.
    
  3. 转到视图的底层CALayer。当您设置视图的 transform 属性时,它会将仿射变换转换为完整的 3D (4x4) 变换矩阵并设置其层的 transform 属性。该层还有另一个属性sublayerTransform,它仅适用于其子层。我相信您可以将sublayerTransform 设置为其transform 的倒数,以有效地使transform 仅适用于您在其上下文中绘制的图形。应该这样做:

    view.transform = ...;
    view.layer.sublayerTransform = CATransform3DInvert(view.layer.transform);
    

【讨论】:

  • 嗨。第一个解决方案不起作用:CGContextRotateCTM(ctx, -M_PI_2);另外两种解决方案有奇怪的效果。还有其他解决方案吗?谢谢!
  • 终于!! CGContextTranslateCTM(ctx, CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextRotateCTM(ctx, DegToRad(-90)); CGContextTranslateCTM(ctx, -CGRectGetMidX(rect), -CGRectGetMidY(rect));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多