0

忍受我,这个很难解释。我希望那里的一些英雄知道这里发生了什么。需要一些历史;

我的一个可可对象,“球”代表一个小图形。它只在一个视图中才有意义。在 Ball 的一些方法中,它要求视图重绘。最重要的是,只要设置了 Ball 的位置参数,它就会要求视图重绘。这是在 setter 中实现的。

正如建议的那样,这是满口的:

在视图.m

- (void)mouseUp:(NSEvent *)theEvent {
    if (![runnerPath isEmpty]) {
        [walkPath removeAllPoints];
        [walkPath appendBezierPath:runnerPath];
        [runnerPath removeAllPoints];

        [[self held] setStep:0];
        [[self held] setPath:walkPath];
        [NSTimer scheduledTimerWithTimeInterval:.01 target:[self held] selector:@selector(pace) userInfo:nil repeats:YES];

        }
}

在鲍尔.m

 - (void)pace { 
    CGFloat juice = 10;
    BOOL loop = YES;

    while (loop) {
        if ([self step] == [[self path] elementCount]) {
            if ([[self timer] isValid]) {
                [[self timer] invalidate];
            }
            [[self path] removeAllPoints];
//          @throw([NSException exceptionWithName:@"test" reason:@"reason" userInfo:nil]);
        }

        if (loop) {
            CGFloat distance;
            NSPoint stepPoint;

            if ([[self path] elementCount] > 0) {
                NSPoint returnPoints[2];
                [[self path] elementAtIndex:[self step] associatedPoints:returnPoints];
                stepPoint = returnPoints[0];
                distance = pixelDistance([self position], stepPoint);
            }

            if (distance <= juice) {
                [self setPosition:stepPoint];
                if (distance < juice) {
                    juice -= distance;
                    loop = YES;
                    [self setStep:[self step]+1];
                } else {
                    loop = NO;
                }
            } else {            
                NSPoint cutPoint = moveAlongBetween([self position], stepPoint, juice);
                [self setPosition:cutPoint];

                loop = NO;
            }

        }
    }

}
4

2 回答 2

1

试着打电话

for (NSView *each in [self views]) {
    ...
}

我假设这views是一个数组,所以快速枚举直接应用于它,不需要调用 allObjects。

其他几点。

  1. 您是否设置了 objc_exception_throw 的全局断点?这将适用于所有 Xcode 项目并且非常有用,我很惊讶它没有默认设置。
  2. 您说您查看了控制台是否有错误。那么,我认为您没有在代码上设置断点并进入它以查看当您的执行到达该点时究竟发生了什么?查看Xcode 调试指南
于 2010-07-27T17:23:36.513 回答
1

你能告诉你如何处理异常吗?因为通常无法识别的选择器会结束您的程序。也许你需要一个例外而不是一个无法识别的选择器。尝试:

@throw([NSException exceptionWithName:@"test" reason:@"reason" userInfo:nil]);

如果这也可以解决它,那么您正在执行此代码后冻结应用程序的操作。

编辑:感谢代码更新。

这里发生了一些奇怪的事情!我不打算重写整个事情,所以这里有一些指针:

  • 首先:您在从计时器循环调用的某个例程中循环。这是故意的吗?没有办法在该while()循环中暂停执行,所以无论如何它都会在眨眼间发生。您需要在课堂上保留一些状态信息。例如,每次pace都调用一个循环计数器。
  • 第二:如果你启动一个定时器,它会以定时器作为参数调用你的选择器。所以将函数定义为-(void)pace:(NSTimer*)timer, 并使用timer, not [self timer](如果你不分配它,后者无论如何都不会是你的计时器!)
  • 第三:你每秒发射 100 次。这是很多,并且可能高于您正在为其编写的任何设备的刷新率。我认为20 /秒就足够了。
  • 第四:可以肯定的是,如果你把它改成-(void)pace:(NSTimer*)timer,不要忘记使用@selector(pace:)(即不要忘记:

修复这些问题,如果仍然损坏,请再次更新您的问题并发表评论,以便我们知道。祝你好运!

于 2010-07-27T17:25:29.183 回答