问题
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];
}
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