在循环遍历 UIButton 时使用layoutsubviews() 可以正常工作,但在通过标签检索它们时循环则不行

发布于 2024-10-11 04:05:14 字数 1379 浏览 1 评论 0原文

我正在尝试模仿瓷砖游戏的 iPhone Springboard 拖放功能,目前正在使用 MatrixCells 在视图初始化中生成 UIButtons 并将它们添加到视图中。 MatrixCell 有一个 order 标记,该标记也用于它们自己的按钮 ([button setTag:[cell order]];)。

当使用“layoutSubviews”循环浏览视图中的 UIButtons 以将它们排列在网格中时,它工作得很好。网格弹出,我的拖动功能允许我将按钮自由拖动到我想要的位置。

<代码> - (void)layoutSubviews {

int row = 0;
int col = 0;
for ( UIButton *button in [self subviews] ) {
    [button setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70)];

    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

但是,在更改单元格的顺序时,循环遍历 subviews 不起作用,因为我很确定我'我根本不应该在那儿乱搞。因此,我想循环遍历我的 MatrixCells 并使用它们的顺序来获取适当的视图,然后更改框架。然而,按钮 0 处的布局变得混乱,我不能再将它们用作按钮。

<代码> - (void)layoutSubviews {

int row = 0;
int col = 0;
for (MatrixCell *cell in currentOrder) {
    UIView *view = [self viewWithTag:[cell order]];
    [view setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70 )];
    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

这是一张结果图片: http://i52.tinypic.com/2q903lk.png

viewWithTag 似乎是最优雅的解决方案。所以我很茫然。知道为什么这些实现的渲染效果不一样吗?

I'm trying to mimic the iPhone Springboard drag and drop functionality for a tile game and am currently using MatrixCells to generate UIButtons in the View initalisation and add them to the view. The MatrixCells have an order tag, which is used for their own button as well ([button setTag:[cell order]];).

When using `layoutSubviews' to cycle through the UIButtons in the view to arrange them in a grid, it works fine. The grid pops up and my dragging functionality allows me to free-drag the buttons where I will.


- (void)layoutSubviews {

int row = 0;
int col = 0;
for ( UIButton *button in [self subviews] ) {
    [button setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70)];

    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

However, cycling through subviews doesn't work when changing the order of the cells, since I'm pretty sure I'm not supposed to mess around in there at all. So, I wanted to cycle through my MatrixCells and use their order to get the appropriate view and then change the frame. However, the layout gets messed up at button 0 and I can't use them as buttons anymore.


- (void)layoutSubviews {

int row = 0;
int col = 0;
for (MatrixCell *cell in currentOrder) {
    UIView *view = [self viewWithTag:[cell order]];
    [view setFrame:CGRectMake( col * 80 + 5, row * 80 + 25, 70, 70 )];
    if (col < 3) {
        col++;
    }
    else {
        row++;
        col = 0;
    }
}

}

Here is a picture with the results: http://i52.tinypic.com/2q903lk.png

viewWithTag seemed the be the most elegant solution for this. So I'm at a loss. Any idea why these implementations don't render the same?

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

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

发布评论

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

评论(2

晚雾 2024-10-18 04:05:14

在我看来,你就像有一个背景 UIImage 来提供灰色渐变背景。看起来您的按钮 0[cell order] 可能为 0。我猜您从未更改过背景 的标签>UIImage,因此默认为 0。

当您为第一个 UIButton(标记为 0)调用 [self viewWithTag] 时,它将找到第一个子视图带有标签 0 - 背景图像,而不是您期望的 UIButton - 并将其移动。这个,我怀疑。这就是为什么所有其他按钮排列得很好,为什么背景移动奇怪,以及为什么你的 0 按钮不可见。

It looks to me rather like you have a background UIImage to provide that grey gradient backdrop. It also looks like your button 0 might have a [cell order] of 0. I'm going to guess that you've never changed the tag of your background UIImage, which will thus default to 0.

When you call [self viewWithTag] for the first UIButton (labeled 0), it will find the first subview with a tag 0 - the background image, not the UIButton you expected - and move that. This, I suspect. is why all the other buttons line up fine, why the background moves oddly, and why your 0 button is not visible.

谜兔 2024-10-18 04:05:14

[self viewWithTag:0] 首先返回 self,而不是预期的 (UIButton *),因为我没有更改视图的 >tag 初始化时。显然,标签在初始化时默认为 0。然后 layoutsubviews 继续更改 selfframe,搞乱了布局和功能。我通过在初始化时将视图的标签更改为 999 解决了这个困境。

[self viewWithTag:0] returns self first, instead of the expected (UIButton *), since I didn't change the view's tag upon initialization. Apparantly tags default to 0 at initialization. layoutsubviews then proceeded to change the frame of self, messing up the layout and functionality. I solved this dilemma by changing the view's tag to 999 at initialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文