如何更改变换的值?

发布于 2024-07-29 13:27:26 字数 402 浏览 2 评论 0原文

例如,我可以像这样访问它们:

self.layer.transform.m32

但我无法为其分配值,就像

self.layer.transform.m32 = 0.3f;

它说无效分配一样。 但这实际上不应该起作用吗?

struct CATransform3D
   {
   CGFloat m11, m12, m13, m14;
   CGFloat m21, m22, m23, m24;
   CGFloat m31, m32, m33, m34;
   CGFloat m41, m42, m43, m44;
};

至少 Xcode 确实能够识别矩阵中的所有这些字段。

For example, I can access them like this:

self.layer.transform.m32

but I can't assign a value to it, like

self.layer.transform.m32 = 0.3f;

it says invalid assignment. But shouldn't that actually work?

struct CATransform3D
   {
   CGFloat m11, m12, m13, m14;
   CGFloat m21, m22, m23, m24;
   CGFloat m31, m32, m33, m34;
   CGFloat m41, m42, m43, m44;
};

at least Xcode does recognize all these fields in the matrix.

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

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

发布评论

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

评论(3

冷︶言冷语的世界 2024-08-05 13:27:26

您访问名为“transform”的属性(CALayer 类),即使用 CATransform3D 参数类型调用 setter 函数。
因此您无法直接访问 CATransform3D 结构成员。

您可能需要先初始化临时变量(CATransform3D 类型),然后将其完全分配给属性。

您尝试以这种方式访问​​的任何属性都会发生同样的情况。

例如:

view.frame.origin.x = 0; //will **not** work for UIView* view

工作示例(通过临时变量):

CATransform3D temp = self.layer.transform; //create  temporary copy
temp.m32 = 0.3f; 
self.layer.transform = temp; //call setter [self.layer setTransform:temp]- mean the same

You access property called "transform" (CALayer class) i.e. call setter function with CATransform3D argument type.
Therefore you cannot access to CATransform3D structure members directly.

You may need to initialize temporary variable first (CATransform3D type) then assign it to property entirely.

Same thing will occur with any property you try to access this way.

For example:

view.frame.origin.x = 0; //will **not** work for UIView* view

Worked sample (via temporary variable):

CATransform3D temp = self.layer.transform; //create  temporary copy
temp.m32 = 0.3f; 
self.layer.transform = temp; //call setter [self.layer setTransform:temp]- mean the same
终止放荡 2024-08-05 13:27:26

我似乎以这种方式访问​​和设置转换没有问题;

CATransform3D t = view.transform;
NSLog(@"t.m32 = %f, t.m34 = %f", t.m32, t.m34);
t.m34 = 100.5;
NSLog(@"t.m32 = %f, t.m34 = %f", t.m32, t.m34);
view.transform = t;

I seem to have no trouble accessing and setting transforms this way;

CATransform3D t = view.transform;
NSLog(@"t.m32 = %f, t.m34 = %f", t.m32, t.m34);
t.m34 = 100.5;
NSLog(@"t.m32 = %f, t.m34 = %f", t.m32, t.m34);
view.transform = t;
¢好甜 2024-08-05 13:27:26

self.layer.transform.m32 = 0.3f; 不会对图层的变换执行任何操作。

self.layer.transform 返回一个 CATransform3D,它不是一个对象。 这意味着它被复制,如果您更改 .m32,您将更改副本,而不是图层的 CATransform3D。

这可行(类似于 mahboudz 的代码示例):

CATransform3D t = self.layer.transform; // t is a copy of self.layer.transform
t.m32 = 0.3f; // modify the copy
self.layer.transform = t; // copy it back into self.layer.transform

self.layer.transform.m32 = 0.3f; won't do anything to the layer's transform.

self.layer.transform returns a CATransform3D, which is not an object. That means it is copied and if you change .m32 you're changing the copy, not the layer's CATransform3D.

This would work (similar to mahboudz' code sample):

CATransform3D t = self.layer.transform; // t is a copy of self.layer.transform
t.m32 = 0.3f; // modify the copy
self.layer.transform = t; // copy it back into self.layer.transform
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文