English 中文(简体)
动态正确定位子视图所需的数学数学
原标题:Math needed to correctly positioning subviews dynamically

这是一个漫长的一天,我的大脑似乎 不想和我合作了...

在一个子视图的数组中,通过每个视图的圈圈插入。每个子视图高度为 100 px。当数组有一个项时,视图的 Y 值需要设置为 0。当数组有两个项时, 索引 0 的视图需要设定为 y 值为 100, 索引 1 的项需要设定 Y 值为 0。 依此 :

1 item: 0 = 0
2 items: 0 = 100, 1 = 0
3 items: 0 = 200, 1 = 100, 2 = 0
4 items: 0 = 300, 1 = 200, 2 = 100, 3 = 0

我需要能够根据数组中的项目数量来动态地正确处理此事项。 以下是我到目前为止拥有的代码 :

for (int i = 0; i < [subViews count]; i++) {
    NSView *v = (NSView *)[subViews objectAtIndex:i];
    [v setFrameOrigin:NSMakePoint(copy.view.frame.origin.x, i * 100)];//This gives me the opposite of what I want...
}

谢谢!

最佳回答
int n = [subViews count];
for (NSView *v in subViews) {
    n--;
    [v setFrameOrigin:NSMakePoint(copy.view.frame.origin.x, n * 100)];
}
问题回答

Insert this before the cycle:
int subviewCount = [subViews count];

[(子视图对象)AtIndex:(子count - i - 1)] 而不是 [子意见对象AtIndex:i]

这项工作将:

y = 100 * ([subViews count] - 1 - i)

另外,FYI, 尝试使用 < code> 的 < /code> 环以下列格式使用 < code> 环 :

for(NSView *thisView in subViews)
{
    int i = [subViews indexOfObject:thisView]; //To get the "i position"
    //The rest of the code can be the same
}

原因是如果子视图为空, 执行 NSView * v = (NSView *) [子视图对象AtIndex: i]; 环将至少运行一次, 并在执行 NSView * v = (NSView *) [Sub意见对象AtIndex:i]; 时崩溃。

for (NSView *ThisView in subview) 将无法执行, 如果子意见为空 。





相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

I am having a problem handling large numbers. I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签