这是更改现有视图的 UIView 框架的最佳方法吗?

发布于 2024-09-27 13:06:09 字数 561 浏览 1 评论 0原文

假设我想暂时更改视图的框架(例如,将其向右移动 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 技术交流群。

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

发布评论

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

评论(3

蘑菇王子 2024-10-04 13:06:09

还有奖励点:为什么我不能直接在视图的框架属性上分配新值,为什么我必须像这样“绕圈子”?

因为点符号在这里用于 2 个不同的用例:属性访问和结构成员访问。另一种写法

self.someView.frame.origin.x += 10.0f;

[[self someView] frame].origin.x += 10.0f;

这一行更清楚地表明这是行不通的。 [[self someView] frame] 检索 CGRect 但如果您要更改此结构的成员,则 frame 属性本身的内容将不被改变。要将某些内容分配给框架属性,必须使用 -setFrame: 方法。并且 -setFrame 始终需要整个 CGRect 作为其参数。

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?

Because the dot notation is used for 2 different use cases here: property access and struct member access. Another way to write

self.someView.frame.origin.x += 10.0f;

would be

[[self someView] frame].origin.x += 10.0f;

This line makes it clearer that this can't work. [[self someView] frame] retrieves a CGRect but if you would change a member of this struct, the contents of the frame property itself would not be changed. To assign something to the frame property, the -setFrame: method must be used. And -setFrame always requires an entire CGRect as its argument.

誰認得朕 2024-10-04 13:06:09

回答这个问题也可以获得奖励积分。这都是关于 getter 和 getter 的。设置器。

self.someView.frame 是一个 getter——它返回框架 &不再深入。它不允许您访问 CGRect 并更改值。

因此:

CGRect r = self.someView.frame;
r.origin.x += 10.0f;
self.someView.frame = r;

毫无疑问,如果来自一种以点表示法为获取 & 的典型方式的语言,这会令人困惑。无论上下文如何设置值。 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:

CGRect r = self.someView.frame;
r.origin.x += 10.0f;
self.someView.frame = r;

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.

迷迭香的记忆 2024-10-04 13:06:09

我在 github 上有一个宏,可以让你像

@morph(self.someView.frame, _.origin.x += 10);

或者如果你想改变x 宽度

@morph(view.frame, {
    _.origin.x += 10;
    _.size.width -= 10;
});

I've got a macro on github that will let you do it like

@morph(self.someView.frame, _.origin.x += 10);

Or if you wanted to change x and width

@morph(view.frame, {
    _.origin.x += 10;
    _.size.width -= 10;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文