Check UIImageView array frame equality with NSTimer

大憨熊 提交于 2019-12-11 23:46:44

问题


I am wondering how to check if all the frames of two UIImageView NSMutableArrays are equal to each other. Now I am using a NSTimer for that.

Here is the code which I used in the method:

__block BOOL equal = YES;
[Img1Array enumerateObjectsUsingBlock:^(UIImageView *ImageView1, NSUInteger idx, BOOL *stop) {
    UIImageView *ImageView2 = Img2Array[idx];
    if (!CGRectEqualToRect(ImageView1.frame, ImageView2.frame)) {
        *stop = YES;
        equal = NO;
    }
}];

if (equal) {
    NSLog(@"ALL THE FRAMES ARE EQUAL");
    [AllPosCorrectTimer invalidate];
}

The method has a boolean in it as you can see. But every time the value of the 'equal' boolean is true because of the timer, so the frames are always equal to each other according to the if-statement.

The function has a boolean in it as you can see. But every time the value of the 'equal' boolean is true because of the timer, so the frames are always equal to each other according to the if-statement.

How can I make sure this method will work?


回答1:


The block is called a-synchronically, that means that the if statement will be called every time with the equal=YES.

Try using the regular enumeration:

- (void)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkTheFrames) userInfo:nil repeats:YES];
}  

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

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


来源:https://stackoverflow.com/questions/17770973/check-uiimageview-array-frame-equality-with-nstimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!