myView.frame.origin.x = 值;不起作用 - 但为什么? [英] myView.frame.origin.x = value; does not work - But why?

查看:17
本文介绍了myView.frame.origin.x = 值;不起作用 - 但为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不能使用这个:

I know that I can't use this:

myView.frame.origin.x = 25.0;

而我必须用它来代替:

CGRect myFrame = myView.frame;
myFrame.origin.x = 25.0;
myView.frame = myFrame;

而且我一直都在这样做,但我不知道为什么我必须这样做.我想填补我理解上的空白.有人可以解释一下吗?

And I'm doing it all the time, but I don't know why I must do it that way. I would like to fill that gap in my understanding. Can someone explain ?

如今 Xcode 为您提供表达式不可分配".前段时间你收到一个编译错误左值需要作为赋值的左操作数".

Nowadays Xcode gives you "Expression not assignable". Some time ago you got a compile error "Lvalue required as left operand of assignment".

推荐答案

这不起作用的原因是由于混合了两种语法.
首先你有."作为调用访问器函数的快捷方式(Objective-C 功能).所以

The reason this does not work is due to the mixing of two syntaxes.
First you have "." as a shortcut for calling the accessor functions (an Objective-C feature). So

  • a.b 变成 [a getB];
  • a.b = 5 变成 [a setB:5];

然后是."作为直接结构成员访问(纯 C).所以

And then theres "." as direct struct member access (pure C). So

  • a.b 确实是 a.b;
  • a.b 真的是 a.b = 5;

将其组合到这样的设置值案例中是行不通的.
因为 ...如果你能打电话

Combining this in a set-value-case like this, doesn't work.
Because ... If you could call

myView.frame.origin.x = 25.0;

  • myView.frame"部分等于 [myView getFrame] 并且你得到一个复制的 CGRect 框架(一个 C 结构)

    • The "myView.frame" part equals [myView getFrame] and you get a copied CGRect frame (a C struct)

      myView.frame.origin"为您提供复制的 CGRect 的 CGPoint 原点(也是一个结构体)

      The "myView.frame.origin" gives you a CGPoint origin (also a struct) of the copied CGRect

      myView.frame.origin.x = 25.0"给你一个原点的 CGFloat x,现在你想给它分配一些东西,问题来了......

      The "myView.frame.origin.x = 25.0" gives you a CGFloat x of the origin and now you want to assign something to it and here comes the problem...

      你尝试设置一个struct的struct的变量,没问题,但是没有从UIView到struct的指针,所以它被复制了.所以你复制,然后你设置,然后你期望设置的动作通过初始获取以某种方式转发到 UIView,好吧,这只是行不通.

      You try to set a variable of a struct of a struct, which is ok, but there is no pointer from the UIView to the struct, so it is copied instead. So you copy and then you set and then you expect that the set action is somehow forwarded through the initial get to the UIView, well and this just doesn't work.

      当然,您可能想知道为什么 Apple 不只是创建一个快捷方式,以便最终您复制的框架会自动重新注入到自动附加的 setFrame 调用中,我想您只需要接受它的现状即可.

      Of course you could wonder why Apple hasn't just created a shortcut, so that in the end your copied frame is automatically reinjected into a auto appended setFrame call, I guess you just have to live with how it is.

      所以记住,如果你得到一个指向框架的指针,它会起作用,但你没有,而是得到一个复制的结构.
      因此,如果您希望 myView.frame.origin.x = 25.0 工作,您间接希望您的调用会自动转换为某种
      [myView setFrame:[myView getFrame:frame].origin.x = 25.0].
      好吧,我想你可以承认这看起来不对.

      So remember, it would work if you'd get a pointer to the frame, but you don't, you get a copied struct instead.
      So if you expect myView.frame.origin.x = 25.0 to work you indirectly expect your call to be automagically translated into some sort of
      [myView setFrame:[myView getFrame:frame].origin.x = 25.0].
      Well I guess you can admit that this looks wrong.

      还想象一下,如果您获得一个指向 CGRect 框架的直接指针,并且您将通过该指针更改某些内容,那么 UIView 将如何知道它的大小已更改并且必须自行更新?另一方面,如果进行了 [myView setFrame:newFrame] 调用,则 UIView 可以自行进行所有必要的重新调整.

      Also imagine if you'd get a direct pointer to the CGRect frame and you'd change something through that pointer, how would the UIView know that it's size has changed and that it has to update itself ? If on the other hand a [myView setFrame:newFrame] call is made, then UIView can do all the necessary readjusting itself.

      这篇关于myView.frame.origin.x = 值;不起作用 - 但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆