-4

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?

Steven
  • 1
  • 4

2 Answers2

0

try like this,here every time equal value is YES because your if block executes before the completion of block execution,in this type of situations you need to take care of block execution.

-(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];

            }
            }
        }];

    }
Balu
  • 8,470
  • 2
  • 24
  • 41
  • Unfortunately that doesn't work. Now I don't see a `NSLog` when all labels are matching. – Steven Jul 22 '13 at 10:01
  • 1
    Sorry, but that does not make any sense. `enumerateObjectsUsingBlock` works *synchronously* and does not return before all array objects have been enumerated. – Martin R Jul 22 '13 at 10:01
  • 1
    @Steven: No, because I don't know your data. You should add NSLog statements that print all the relevant data (frames etc) and add that information to your *question*. – Martin R Jul 22 '13 at 11:02
  • I think the data doesn't matter. I already have checked if the labels are matching manually. That works. Also are the frames the same size. – Steven Jul 22 '13 at 11:07
  • 1
    @Steven: It is the same as with your previous questions. You *ignore all advice* given by others and just say "I already have checked it". If you don't provide detailed data then nobody can solve the problem (only by guessing). – Martin R Jul 22 '13 at 11:18
0

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).

Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92