ios - CGRect 具有空值?
<p><p>我现在像这样在我的 ViewController 中生成我的 View :</p>
<pre><code>-(void) generateCardViews {
int positionsLeftInRow = _CARDS_PER_ROW;
int j = 0; // j = ROWNUMBER (j = 0) = ROW1, (j = 1) = ROW2...
for (int i = 0; i < ; i++) {
NSInteger value = ((CardModel *)self.gameModel.cards).value;
CGFloat x = (i % _CARDS_PER_ROW) * 121 + (i % _CARDS_PER_ROW) * 40 + 45;
if (j % 2) {
x += 80; // set additional indent (horizontal displacement)
}
CGFloat y = j * 85 + j * 40 + 158;
CGRect frame = CGRectMake(x, y, 125, 125);
CardView *cv = [ initWithFrame:frame andPosition:i andValue:value];
</code></pre>
<p>我从以前的 VC 获得了 _CARDS_PER_ROW 的数据。 </p>
<p>但现在我想把一些地方留空。</p>
<p>现在我可以生成例如:</p>
<pre><code>***
***
***
</code></pre>
<p>但我想生成这样的东西:</p>
<pre><code>***
* *
***
</code></pre>
<p>但我不知道如何跳过某些地方以获取我的观点...</p>
<p>编辑:</p>
<pre><code>- (void)viewDidLoad {
;
level1GameViewController *gvc = ;
gvc.CARDS_PER_ROW = 3;
//gvc.gameStateData = gameStateData;
;
}
</code></pre>
<p>这是我从前一个 Controller 传递的内容</p>
<p>编辑:</p>
<pre><code>****
****
****
******
** **
******
** **
* *
* *
</code></pre>
<p>例如我需要的所有这些安排</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,我们把帧计算放到一个单独的方法中:</p>
<pre><code>- (CGRect)frameForCardViewAtPositionI:(NSUInteger)i positionJ:(NSUInteger)j {
CGRect frame;
frame.size = CGSizeMake(125, 125);
frame.origin.x = i * frame.size.width + 45; //or whatever logic you need here
frame.origin.y = j * frame.size.height + 158;
return frame;
}
</code></pre>
<p>现在,让我们用掩码 block 指定卡片分布,我们将其作为参数发送:</p>
<pre><code>- (void)generateCardViews:(BOOL (^)(NSUInteger, NSUInteger)cardAllowedOnPosition {
NSUInteger viewIndex = 0;
for (NSUInteger index = 0; index < ; index++) {
NSInteger value = ((CardModel *)self.gameModel.cards).value;
NSUInteger i, j;
do {
i = viewIndex % _CARDS_PER_ROW;
j = viewIndex / _CARDS_PER_ROW;
viewIndex++;
} while (!cardAllowedOnPosition(i, j));
CGRect frame = ;
CardView *cv = [ initWithFrame:frame
andPosition:index
andValue:value];
}
</code></pre>
<p>}</p>
<p>称为</p>
<pre><code>[self generateCardViews:^(NSUInteger i, NSUInteger j) {
return !(j == 1 && i > 1 && i < 4);
}];
</code></pre>
<p>生成(假设<code>_CARDS_PER_ROW</code>为<code>6</code>)</p>
<pre><code> ******
** **
******
</code></pre></p>
<p style="font-size: 20px;">关于ios - CGRect 具有空值?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30731580/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30731580/
</a>
</p>
页:
[1]