这是更改现有视图的 UIView 框架的最佳方法吗?
假设我想暂时更改视图的框架(例如,将其向右移动 10px - 可能与突出显示更改有关)。
我假设伪代码会是这样的
self.someView.frame.origin.x += 10.0f;
但这给了我一个错误
lvalue required as left operand of assignment
所以我所做的是创建一个新的 CGRect 来表示框架,改变 CGRect 然后给视图 CGRect 因为它的框架
CGRect aFrame = self.someView.frame;
aFrame.origin.x += 10.0f;
[self.someView setFrame:aFrame];
似乎很有趣,我可以做一个在 CGRect 上赋值,但不在作为 CGRect 的视图框架上赋值。
那么,这是改变现有视图框架的最佳方法吗?
还有奖励点:为什么我不能直接在视图的框架属性上分配新值,为什么我必须像这样绕过灌木丛?
谢谢
Lets say that I want to temporarily change the frame of a view (for eg move it 10px to the right - probably in connection with highlighting a change).
I assumed the psuedo code would be something like
self.someView.frame.origin.x += 10.0f;
But this gives me an error of
lvalue required as left operand of assignment
So what I do instead is make a new CGRect to represent the frame, alter that CGRect then give the view the CGRect as it's frame
CGRect aFrame = self.someView.frame;
aFrame.origin.x += 10.0f;
[self.someView setFrame:aFrame];
Seems funny that I can make an assignment on a CGRect, but not on a view's frame which is a CGRect.
So, is this the best way to alter a frame for an existing view?
And for bonus points: Why can't I assign a new value directly on the view's frame property, why do I have to beat 'round the bushes like this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为点符号在这里用于 2 个不同的用例:属性访问和结构成员访问。另一种写法
是
这一行更清楚地表明这是行不通的。
[[self someView] frame]
检索CGRect
但如果您要更改此结构的成员,则frame
属性本身的内容将不被改变。要将某些内容分配给框架属性,必须使用-setFrame:
方法。并且-setFrame
始终需要整个CGRect
作为其参数。Because the dot notation is used for 2 different use cases here: property access and struct member access. Another way to write
would be
This line makes it clearer that this can't work.
[[self someView] frame]
retrieves aCGRect
but if you would change a member of this struct, the contents of theframe
property itself would not be changed. To assign something to the frame property, the-setFrame:
method must be used. And-setFrame
always requires an entireCGRect
as its argument.回答这个问题也可以获得奖励积分。这都是关于 getter 和 getter 的。设置器。
self.someView.frame 是一个 getter——它返回框架 &不再深入。它不允许您访问 CGRect 并更改值。
因此:
毫无疑问,如果来自一种以点表示法为获取 & 的典型方式的语言,这会令人困惑。无论上下文如何设置值。 Objective C 点表示法可能会以不可预见的方式造成混淆,因为 Obj-C 可以通过点语法调用方法并获取属性值。因此,没有简单的方法可以知道 self.someView.frame 真的是 self.someView.frame() (使用 Pythonic 语法)。
听起来不是很烦人(实际上,我每天都编写 Python 程序),但您实际上并没有使用 Obj-C 的标准消息传递语法来绕圈子。您绝对可以利用点语法来节省一些步骤,但是括号内的消息传递具有一定的自记录帮助,可以帮助您避免此类错误。
Answering this question answers for bonus points, as well. It's all about getters & setters.
self.someView.frame is a getter--it returns the frame & goes no deeper. It doesn't allow you to reach into the CGRect and alter values.
Thus:
This is, no doubt, confusing if coming from a language where dot-notation is the typical way of getting & setting values regardless of context. Objective C dot-notation can be confusing in unforseen ways because Obj-C can invoke methods via dot syntax as well as get property values. Thus, there's no simple way of knowing that self.someView.frame is really self.someView.frame() (to use a Pythonic syntax).
Not to sound annoying (really, I program Python daily), but you're really not beating around the bushes using the standard message-passing syntax of Obj-C. You can definitely leverage dot syntax to save yourself some steps, but bracketed message-passing has a certain amount of self-documenting helpfulness and helps you avoid such errors.
我在 github 上有一个宏,可以让你像
或者如果你想改变
x
和宽度
I've got a macro on github that will let you do it like
Or if you wanted to change
x
andwidth