使用NSTimer检查UIImageView数组框架是否相等 [英] Check UIImageView array frame equality with NSTimer
问题描述
我想知道如何检查两个 UIImageView
NSMutableArray
的所有帧是否彼此相等.现在,我为此使用 NSTimer
.
I am wondering how to check if all the frames of two UIImageView
NSMutableArray
s 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];
}
如您所见,该方法中包含一个布尔值.但是由于计时器的缘故,每次等于"布尔值的值都为true时,根据if语句,帧始终彼此相等.
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.
如您所见,该函数中有一个布尔值.但是由于计时器的缘故,每次等于"布尔值的值都为true时,根据if语句,帧始终彼此相等.
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?
推荐答案
该块被a同步调用,这意味着 if
语句每次都用equal = YES调用.
The block is called a-synchronically, that means that the if
statement will be called every time with the equal=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;
}
这篇关于使用NSTimer检查UIImageView数组框架是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!