【问题标题】:See NSLog at wrong time在错误的时间查看 NSLog
【发布时间】:2013-07-22 09:51:19
【问题描述】:

我有两层。底层由隐藏的 UIImageViews 组成,上层由可见的 UIImageViews 组成。他们有标签。当底层 UIImageViews 的所有帧都等于上层 UIImageViews 的帧并且标签也匹配时,您必须在 NSLog 中看到这一点。问题是当所有标签都不匹配时,我仍然得到 NSLog。该方法由NSTimer 调用。

这是我的代码:

-(void)allPiecesCorrectPos {

        __block BOOL equal = YES;
        [arrayImg enumerateObjectsUsingBlock:^(UIImageView *ImageView1, NSUInteger idx, BOOL *stop) {
            UIImageView *ImageView2 = HiddenFieldView[idx];
            if (CGRectIntersectsRect(ImageView1.frame, ImageView2.frame) && ImageView1.tag != ImageView2.tag) {

                equal = NO;
                *stop = YES;
            }
        }];
        if (equal) {
            NSLog(@"ALL THE FRAMES ARE EQUAL");
            [AllPosCorrectTimer invalidate];

        }
    }

我该如何解决这个问题?

【问题讨论】:

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


【解决方案1】:

您的代码没有按照您在文本中所说的去做。您正在测试 UIImageView 框架的交叉点而不是相等性。试试这个(我冒昧地重命名了一些方法和变量以便于阅读):

-(void)visiblePiecesEquivalentToHiddenPieces
{
    __block BOOL correspondingPiecesEquivalent = YES;
    [imageArray enumerateObjectsUsingBlock:
        ^(UIImageView *imageView1, NSUInteger index, BOOL *stop)
        {
            UIImageView *imageView2 = hiddenFieldView[index];
            if ( !CGRectEqualToRect(imageView1.frame, imageView2.frame)
                 || imageView1.tag != mageView2.tag )
            {    
                correspondingPiecesEquivalent = NO;
                *stop = YES;
            }
        }
    ];
    if (correspondingPiecesEquivalent)
    {
        NSLog(@"Frames and tags are equal for corresponding views.");
        [pieceCorrespondenceTimer invalidate];

    }
}

阅读有关逻辑条件及其组合方式的更多信息。如果这不是您想要的,请澄清您的问题。

我还建议您停止以大写开头的变量名称。以大写开头的名称最常见的是 class 名称。大写开头的变量名对于 Objective-C 来说是一种非常不常见的命名约定,它会使你的代码更难被其他程序员阅读。 (但如果您出于任何原因更愿意这样做,请继续。只要保持一致和连贯即可)。

【讨论】:

    【解决方案2】:

    像这样尝试,这里每次相等的值都是YES,因为你的if块在块执行完成之前执行,在这种情况下你需要注意块执行。

    -(void)allPiecesCorrectPos {
    
            __block BOOL equal = YES;
            [arrayImg enumerateObjectsUsingBlock:^(UIImageView *ImageView1, NSUInteger idx, BOOL *stop) {
                UIImageView *ImageView2 = HiddenFieldView[idx];
                if (CGRectIntersectsRect(ImageView1.frame, ImageView2.frame) && ImageView1.tag != ImageView2.tag) {
    
                    equal = NO;
                    *stop = YES;
                if (equal) {
                NSLog(@"ALL THE FRAMES ARE EQUAL");
                [AllPosCorrectTimer invalidate];
    
                }
                }
            }];
    
        }
    

    【讨论】:

    • 不幸的是,这不起作用。现在,当所有标签都匹配时,我看不到 NSLog
    • 对不起,但这没有任何意义。 enumerateObjectsUsingBlock同步工作,在枚举所有数组对象之前不会返回。
    • @Steven:不,因为我不知道你的数据。您应该添加打印所有相关数据(帧等)的 NSLog 语句,并将该信息添加到您的 question
    • 我认为数据无关紧要。我已经检查了标签是否手动匹配。这样可行。框架的大小也是一样的。
    • @Steven:和你之前的问题一样。你忽略其他人的所有建议,只说“我已经检查过了”。如果您不提供详细数据,那么没有人可以解决问题(只能通过猜测)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2011-06-25
    相关资源
    最近更新 更多