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

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

问题描述

我知道我不能使用它:

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给您表达式不可分配".不久前,您收到一个编译错误需要Lvalue作为赋值的左操作数".

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];

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

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

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

在这样的set-value-case中结合使用这是行不通的.
因为 ... 如果可以的话

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...

      您尝试设置一个结构的结构变量,这是可以的,但是没有UIView指向该结构的指针,因此将其复制.因此,您先复制然后进行设置,然后您希望将设置操作以某种方式通过初始获取转发到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天全站免登陆