【问题标题】:CGContextConcatCTM vs CALayer affineTransformCGContextConcatCTM vs CALayer affineTransform
【发布时间】:2011-09-26 09:15:51
【问题描述】:

我正在尝试更改 CALayer 的仿射变换,因此其中绘制的内容会变形。

首先我在没有 CALayers 的情况下执行此操作,只是使用传递给 CGContextConcactCTM 的不同 CGAffineTransform 重绘。

这行得通,但速度不够快,所以我决定用 CALayer 来做,这样它就不会每次都重绘。它只是被改变了。

问题在于设置 myLayer.affineTransform = myTransform;似乎和用 CGContextConcatCTM(myTransform); 重绘效果不一样;

我是 Objective-C 的新手,数学很差,所以我确定我做错了什么。但我看不到什么。

谢谢。

【问题讨论】:

  • 好的,我找到了答案,但是问了之后好像8小时都答不上来……

标签: objective-c quartz-graphics affinetransform


【解决方案1】:

好吧,没关系,我发现了。

原来 myLayer.affineTransform 相对于图层中心进行变换,而 CGContextContactCTM 相对于原点进行变换。

所以我只是连接了其他 2 个转换:

    CGPoint center;
    center.x = capa.bounds.origin.x + capa.bounds.size.width/2;
    center.y = capa.bounds.origin.y + capa.bounds.size.height/2;        
    CGAffineTransform trf1 = CGAffineTransformMakeTranslation(center.x, center.y);
    CGAffineTransform trf2 = CGAffineTransformMakeTranslation(-center.x, -center.y);        
    capa.affineTransform = CGAffineTransformConcat(trf1, CGAffineTransformConcat(mat, trf2));      

【讨论】:

  • 您也可以接受自己的答案。如果您想知道什么是接受,请阅读FAQ
猜你喜欢
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多