I have two layers. The bottom layer consists of hidden UIImageViews, the upper layer consists of visible UIImageViews. They have labels in it. When all the frames of the bottom layer UIImageViews are equal to the frames of the upper layer UIImageViews and the labels are also matching, you have to see that in a NSLog. The problem is that when all the labels are not matching, I still get the NSLog. The method is called by a NSTimer
.
This is my code:
-(void)allPiecesCorrectPos {
__block BOOL equal = YES;
[arrayImg enumerateObjectsUsingBlock:^(UIImageView *ImageView1, NSUInteger idx, BOOL *stop) {
UIImageView *ImageView2 = HiddenFieldView[idx];
if (CGRectIntersectsRect(ImageView1.frame, ImageView2.frame) && ImageView1.tag != ImageView2.tag) {
equal = NO;
*stop = YES;
}
}];
if (equal) {
NSLog(@"ALL THE FRAMES ARE EQUAL");
[AllPosCorrectTimer invalidate];
}
}
How can I solve this?
Your code doesn't do what you say you want to do in your text. You are testing the UIImageView frames for intersection not for equality. Try this (I took the liberty to rename some methods and variables for easier reading):
-(void)visiblePiecesEquivalentToHiddenPieces
{
__block BOOL correspondingPiecesEquivalent = YES;
[imageArray enumerateObjectsUsingBlock:
^(UIImageView *imageView1, NSUInteger index, BOOL *stop)
{
UIImageView *imageView2 = hiddenFieldView[index];
if ( !CGRectEqualToRect(imageView1.frame, imageView2.frame)
|| imageView1.tag != mageView2.tag )
{
correspondingPiecesEquivalent = NO;
*stop = YES;
}
}
];
if (correspondingPiecesEquivalent)
{
NSLog(@"Frames and tags are equal for corresponding views.");
[pieceCorrespondenceTimer invalidate];
}
}
Read more about logic conditions and how they combine. If this is not what you want, clarify your question.
I also suggest you stop starting your variable names with uppercase. Uppercase-starting names are most frequently class names. Uppercase starting variable names is a quite uncommon naming convention for Objective-C and it can make your code more difficult to read for other programmers. (But if you prefer to do so for whatever reason, go ahead. Just be consistent and coherent).