【问题标题】:Check UIImageView array frame equality with NSTimer使用 NSTimer 检查 UIImageView 数组帧是否相等
【发布时间】:2013-07-21 09:43:59
【问题描述】:

我想知道如何检查两个UIImageViewNSMutableArrays 的所有帧是否彼此相等。现在我为此使用NSTimer

这是我在方法中使用的代码:

__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];
}

如您所见,该方法中有一个布尔值。但是每次'equal'布尔值的值都是真的,因为计时器,所以根据if语句,帧总是彼此相等。

如您所见,该函数中有一个布尔值。但是每次'equal'布尔值的值都是真的,因为计时器,所以根据if语句,帧总是彼此相等。

如何确保此方法有效?

【问题讨论】:

    标签: objective-c cocoa-touch uiimageview nsarray nstimer


    【解决方案1】:

    该块被异步调用,这意味着if 语句将在每次等于=YES 的情况下被调用。

    尝试使用正则枚举:

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

    【讨论】:

    • 所以我必须将 if 语句放在块中的另一个 if 语句之下?
    • 因为那行不通。当我移动一个UIImageView 时,我多次收到NSLog。当两个UIImageView 数组的所有帧彼此相等时,我想要一个NSLog
    • 在这种情况下我不会使用块枚举,我只会使用常规枚举。查看我更新的答案。
    • 我使用了你的代码,但那不起作用。我立即得到了 NSLog。
    • 如果你得到的 NSLog 意味着其中一个帧不相等,你确定它们都相等吗? (顺便说一句,我的方法有错误,我修复之前不确定你是否复制它,所以重新尝试复制)
    猜你喜欢
    • 2020-02-12
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多