CALayer 锚点无法按预期与 CGAffineTransform 一起工作

发布于 2024-12-07 21:24:48 字数 970 浏览 7 评论 0原文

我有一个 CALayer,包含几个子层(CATextLayers)。 我在 CALayer 上应用一些常用手势(缩放、平移)的转换。 一切都工作得很好,除了缩放(缩放)是向左下角而不是屏幕中心进行的。

我在 CoreAnimation 编程指南中找到了一些信息,并尝试将锚点设置为屏幕中心,以便每个转换都会朝着该锚点完成......但它似乎没有按预期工作。

这是我的代码:

在 init 函数内部:

_layerMgr = [[CALayer alloc] init];
_layerMgr.frame = [[UIScreen mainScreen] bounds];
_layerMgr.anchorPoint = CGPointMake(0.5, 0.5);

在手势事件上完成:

CGAffineTransform tt = CGAffineTransformMakeTranslation( offset.x, -offset.y); 
CGAffineTransform st = CGAffineTransformMakeScale( scale ,scale );
CGAffineTransform rt = CGAffineTransformMakeRotation(rotation); 
transform = CGAffineTransformConcat( tt, CGAffineTransformConcat(st,rt) );
_layerMgr.transform = CATransform3DMakeAffineTransform(_transform);

也许问题来自我的子视图?我已经使用 addSubView 方法添加了它们......没什么花哨的......

I have a CALayer, containing few sublayers (CATextLayers).
I'm applying transformations on the CALayer on some usual gestures (zoom, pan).
Everything works perfectly fine except that the scaling (zoom) is done towards the bottom left corner instead of the center of the screen.

I found some info in the CoreAnimation programming guide and I tried setting the anchor point to the center of my screen so that every transformation would be done towards that anchor....But it doesn't seem to be working as expected.

Here's my code :

inside the init function :

_layerMgr = [[CALayer alloc] init];
_layerMgr.frame = [[UIScreen mainScreen] bounds];
_layerMgr.anchorPoint = CGPointMake(0.5, 0.5);

done on gesture events :

CGAffineTransform tt = CGAffineTransformMakeTranslation( offset.x, -offset.y); 
CGAffineTransform st = CGAffineTransformMakeScale( scale ,scale );
CGAffineTransform rt = CGAffineTransformMakeRotation(rotation); 
transform = CGAffineTransformConcat( tt, CGAffineTransformConcat(st,rt) );
_layerMgr.transform = CATransform3DMakeAffineTransform(_transform);

Maybe the problem comes from my subviews ? I've added them using the addSubView method...nothing fancy...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2024-12-14 21:24:48

尝试在“您的”_layerMgr.transform 上进行转换,而不是创建一个新的:

例如:

[_layerMgr setTransform:CGAffineTransformScale (_layerMgr.transform, 2, 2)];

try to transform on "YOUR" _layerMgr.transform instead of creating a new one:

for example:

[_layerMgr setTransform:CGAffineTransformScale (_layerMgr.transform, 2, 2)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文