0

I have two layers. The bottom layer consists of hidden UIImageViews, the upper layer consists of visible UIImageViews. When all the frames of the bottom layer UIImageViews are equal to the frames of the upper layer UIImageViews, you have to see that in a NSLog.

The problem is that the boolean method which is called by a NSTimer always returns true immediately, so I see the NSLog. I only want to see the NSLog when all corresponding frames are equal to each other.

This is my code:

- (void)checkTheFrames {
    BOOL allEquals = [self isEqualFrames];
    if (allEquals) {
        NSLog(@"ALL THE FRAMES ARE EQUAL");
        [AllPosCorrectTimer invalidate];
    }
}

-(BOOL)isEqualFrames {
    for(int i = 0; i < arrayImg.count; i++ ){
        UIImageView *ImageView1 = arrayImg[i];
        UIImageView *ImageView2 = HiddenFieldView[i];
        if (!CGRectEqualToRect(ImageView1.frame, ImageView2.frame)) {
            return NO;
        }
    }
    return YES;
}

Is there a way to solve this issue?

Steven
  • 1
  • 4
  • You should log the frames to make sure you're getting what you think you should in the isEqualFrames method. – rdelmar Jul 21 '13 at 16:28
  • I did, all the frames are equal when they have to be. – Steven Jul 21 '13 at 16:29
  • 1
    And how about when they're not supposed to be equal, have you logged them then? – rdelmar Jul 21 '13 at 16:31
  • Why do I have to do that? – Steven Jul 21 '13 at 16:32
  • Just for troubleshooting purposes. Your logic looks correct in the code you posted, so the only thing I can think of that would be wrong, is the data (frames) coming in to the method. – rdelmar Jul 21 '13 at 16:34
  • I don't understand the question. Your method returns TRUE if all the respective frames are equal. What do you mean by *"always returns true immediately"*, and how is this related to the timer? - In other words: What result do you expect? – Martin R Jul 21 '13 at 16:34
  • The frames are correct. I just logged them. – Steven Jul 21 '13 at 16:35
  • So you're saying that you can log frames that are not all equal, and you still get the method returning YES? – rdelmar Jul 21 '13 at 16:37
  • @MartinR The method only must return true when all the corresponding frames are equal to each other. When the timer calls the method, I immediately see the `NSLog`. – Steven Jul 21 '13 at 16:37
  • @rdelmar Yes, that's right. Very strange I know. – Steven Jul 21 '13 at 16:39
  • @Steven: But the function does exactly that. The method returns TRUE if all corresponding frames are equal, and FALSE otherwise. – Martin R Jul 21 '13 at 16:41
  • Yes, I know. But I see the opposite. – Steven Jul 21 '13 at 16:45
  • Here is my main question where I have posted the whole code I am using now: http://stackoverflow.com/questions/17770973/check-uiimageview-array-frame-equality-with-nstimer/17771226?noredirect=1#comment25924026_17771226 – Steven Jul 21 '13 at 16:47
  • Could be a rounding error which isn't showing when you log the frames. Try rounding the `CGRect` elements to a specified number of decimal places using the answer provided here: http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c – trojanfoe Jul 21 '13 at 16:55
  • Post the logs that you take and show how you changed the code to generate the logging. – Wain Jul 21 '13 at 16:56
  • I think you're going to have to post the actual app somewhere if you want to get an answer. I don't see anything wrong with the logic, or anything you posted in the main question. – rdelmar Jul 21 '13 at 16:57
  • @rdelmar Me neither. I explained the stuff that happens. – Steven Jul 21 '13 at 17:05
  • @Wain this is the NSLog I got: 2013-07-21 19:14:41.580 app2[14208:c07] ALL THE FRAMES ARE EQUAL 2013-07-21 19:14:42.661 app2[14208:c07] 27.968002 73.599998 - 27.968002 73.599998 2013-07-21 19:14:47.789 app2[14208:c07] 116.546005 73.599998 - 116.546005 73.599998 2013-07-21 19:14:50.677 app2[14208:c07] 205.124008 73.599998 - 205.124008 73.599998 – Steven Jul 21 '13 at 17:21
  • 2013-07-21 19:14:54.998 app2[14208:c07] 116.546005 164.350006 - 116.546005 164.350006 2013-07-21 19:15:01.869 app2[14208:c07] 205.124008 164.350006 - 205.124008 164.350006 2013-07-21 19:15:13.381 app2[14208:c07] 116.546005 255.100006 - 116.546005 255.100006 2013-07-21 19:15:17.117 app2[14208:c07] 27.968002 255.100006 - 205.124008 164.350006 2013-07-21 19:15:19.181 app2[14208:c07] 205.124008 345.850006 - 205.124008 345.850006 – Steven Jul 21 '13 at 17:21
  • This is the code I used to get the log: NSLog(@"%f %f - %f %f", touch.view.frame.origin.x, touch.view.frame.origin.y, HiddenView2.frame.origin.x, HiddenView2.frame.origin.y); – Steven Jul 21 '13 at 17:23
  • Where are you logging this? Why are you logging touch.view.frame instead of ImageView1.frame like you have in your method above? – rdelmar Jul 21 '13 at 17:27
  • I am logging this when the touch ended of the UIImageView – Steven Jul 21 '13 at 17:32
  • I have posted the whole code in the main question – Steven Jul 21 '13 at 17:33
  • Are you sure `arrayImg` is not nil or empty? Did you initialize it and add objects to it? – Kurt Revis Jul 21 '13 at 17:58
  • @KurtRevis The array is not empty. It is filled with Uiimageviews – Steven Jul 21 '13 at 18:26
  • 1
    Your log should be: NSLog(@"%f %f - %f %f", ImageView1.frame.origin.x, ImageView1.frame.origin.y, ImageView2.frame.origin.x, ImageView2.frame.origin.y); and it should be placed right before if (!CGRectEqualToRect(ImageView1.frame, ImageView2.frame)) in isEqualFrames. This is where you have the problem, so this is where you should do the log, and it should be done with the actual pointers that you're using in the comparison. Please do that, and report the results. – rdelmar Jul 21 '13 at 20:45
  • @rdelmar This is the log I got: http://we.tl/CcZGb8Esfl – Steven Jul 23 '13 at 15:59

1 Answers1

1

I think whats wrong is that you're comparing the Xs and Ys too... maybe you should go further to frame.size and compare them. Or maybe compare the widths and heights easily (frame1.size.width == frame2.size.width)

Let me know if this didn't solve the problem!

Albara
  • 1,306
  • 8
  • 14
  • The size is a part of a frame. I don't know what you mean – Steven Jul 21 '13 at 18:31
  • @Steven Sorry, thought you just wanted to check the equality of sizes... So you need to compare the locations too? Are they on top of each other when you first fire the method? – Albara Jul 22 '13 at 03:23
  • The locations of the UIImageViews are next to each other. – Steven Jul 23 '13 at 16:05