我正在创建一个拼图应用程序。触摸每一件作品时,我们的想法是,如果布尔(droppedInPlace
)放在正确的位置,则{(1}})被分配YES
;如果放在错误的位置,则NO
。然后我想我需要创建一个包含40个bool(拼图块数)的数组,这些条目中的每一个都将初始设置为NO。
然后使用droppedInPlace
将此bool(replaceObjectAtIndex
)插入到数组中。然后我想循环遍历这个数组。如果所有条目都为真/是/ 1,则拼图完成,我将运行一些代码。
我在做这件事时遇到了一些麻烦。我无法弄清楚如何在上面编写我的想法。以下是我的代码:
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
// filename to be read from
NSString *filePath = [[NSBundle mainBundle]
pathForResource:
@"jigsawHomeCenterPoints"
ofType:@"plist"];
// array of cgpoints in string format
NSArray *jigsawHomeCentersArray = [NSArray arrayWithContentsOfFile:filePath];
// the tag number of the selected image
int tag = touchedView.tag;
NSLog(@"tag: %i", tag);
// says that the piece has not been dropped in it's holder
bool droppedInPlace = NO;
// if what has been touched is part of the array of images
if ([imagesJigsawPieces indexOfObject:touchedView] != NSNotFound) {
// preparing new snap-to-center position
// using (tag-1) as an index in the HomeCentersArray corresponds to where the image should be placed
// this was a conscious decision to make the process easier
CGPoint newcenter = CGPointFromString([jigsawHomeCentersArray objectAtIndex:tag-1]);
// the location of the current touch
CGPoint location = [touch locationInView:self.view];
// call the animate method upon release
[self animateReleaseTouch:touchedView withLocation:location];
// setting a proximity range - if it is within this 80x80 area the image will snap to it's corresponding holder (HomeCenter)
if (location.x > newcenter.x-40 && location.x < newcenter.x+40
&& location.y > newcenter.y-40 && location.y < newcenter.y+40) {
touchedView.center = newcenter;
touchedView.alpha = 1.0;
droppedInPlace = YES;
} else {
touchedView.alpha = 0.5;
droppedInPlace = NO;
}
NSLog(@"True or false: %i", droppedInPlace);
[self checkJigsawCompleted:droppedInPlace withTag:tag];
}
}
检查功能:
-(void) checkJigsawCompleted:(BOOL)inPlace withTag:(NSInteger)tag {
NSMutableArray *piecesInPlace;
[piecesInPlace replaceObjectAtIndex:tag-1 withObject:[NSNumber numberWithBool:inPlace]];
//code to loop through array - check if all entries are true/yes/1
//if so, jigsaw is completed - run some other code
//else, do nothing
}
答案 0 :(得分:2)
请注意,您的piecesInPlace
数组未在方法checkJigsawCompleted
中初始化,并且会在下一行崩溃。
假设它使用正确的值初始化,检查所有值是否为真的方法很简单:
for (NSNumber *boolNumber in piecesInPlace) {
if (![boolNumber boolValue])
return false;
}
return true;
编辑:或更好:
return ![piecesInPlace containsObject:[NSNumber numberWithBool:NO]];
答案 1 :(得分:1)
您想知道如何遍历数组吗?你可以使用for(;;)
循环或for(.. in ..)
循环,或者我喜欢的这类东西:-enumerateObjectsUsingBlock:
__block jigsawIsComplete = true;
[piecesInPlace enumerateObjectsUsingBlock: ^(id obj, NSUInteger i, BOOL* stop)
{
jigsawIsComplete = jigSawIsComplete && [obj boolValue];
*stop = !jigsawIsComplete;
}];
答案 2 :(得分:1)
所以,有些事情。首先,你要根据拼图块的标签替换这些piecesInPlace可变数组中的对象,它看起来像。但是,您永远不会设置这些视图的标记。所以,这一行:
int tag = touchedView.tag;
总是为0.更糟糕的是,你的replaceObjectAtIndex:tag-1位将尝试访问数组的-1索引(此时,但Apple将公开更改一些)合法。
只要循环遍历数组并检查真值,我建议使用方法
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
使用此功能,只要在数组中找到NO值并且枚举结束,就可以将停止指针设置为YES。只要stop为NO,您只想继续枚举。如果您在不停止的情况下通过阵列,则所有值均为YES。