0

我有这种方法不能按我需要的方式工作。它创建了一个 MKPolyline “动画”:假设我有 85 分。连接这些点的每条折线都是在前一条折线之后 0.1 秒创建的。为此,它通过 performSelector:withObject:afterDelay: 调用自身。

-(void)addOverlaysFromPointsWithStartFromAndPoints:(NSArray *)arguments
{

    NSLog(@"end: %@ / coordinates count: %d",[arguments objectAtIndex:0],[[arguments objectAtIndex:1] count]);

    CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D)*2);
    Location *loc1 = (Location *)[[arguments objectAtIndex:1] objectAtIndex:[(NSNumber *)[arguments objectAtIndex:0] intValue]-1];
    Location *loc2= (Location *)[[arguments objectAtIndex:1] objectAtIndex:[(NSNumber *)[arguments objectAtIndex:0] intValue]];
    locations[0] = loc1.location;
    locations[1] = loc2.location;
    routeLine = [MKPolyline polylineWithCoordinates:locations count:2];

    [self.map addOverlay:routeLine];

    if(([(NSNumber *)[arguments objectAtIndex:0] intValue]+1) < [[arguments objectAtIndex:1] count])//add more overlays after delays unless this is the endpoint
    {
        NSArray *parameters = [NSArray arrayWithObjects:[NSNumber numberWithInt:[(NSNumber *)[arguments objectAtIndex:0] intValue] + 1],[arguments objectAtIndex:1],nil];
        [self performSelector:@selector(addOverlaysFromPointsWithStartFromAndPoints:) withObject:parameters afterDelay:0.1];
    }

}

就我而言,我要创建 3 个子路由,所以最初我有一个数组,每个数组有 7、68 和 85 个索引。检查此日志:

2013-02-21 10:31:22.372 SIGView[329:907] end: 1 / coordinates count: 7
2013-02-21 10:31:22.433 SIGView[329:907] end: 1 / coordinates count: 68
2013-02-21 10:31:22.528 SIGView[329:907] end: 1 / coordinates count: 85
2013-02-21 10:31:22.541 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.542 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.650 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.653 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.655 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.796 SIGView[329:907] end: 3 / coordinates count: 85
2013-02-21 10:31:22.798 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.801 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.898 SIGView[329:907] end: 4 / coordinates count: 85
2013-02-21 10:31:22.905 SIGView[329:907] end: 5 / coordinates count: 85
2013-02-21 10:31:22.909 SIGView[329:907] end: 5 / coordinates count: 85
2013-02-21 10:31:23.013 SIGView[329:907] end: 5 / coordinates count: 85
(...)

我似乎在第一次为每个数组调用该方法时,它工作正常,但它假定最后一个数组(包含 85 个对象)。所以,我只有一个创建和动画的“路线”。

日志应类似于:

2013-02-21 10:31:22.372 SIGView[329:907] end: 1 / coordinates count: 7
2013-02-21 10:31:22.433 SIGView[329:907] end: 1 / coordinates count: 68
2013-02-21 10:31:22.528 SIGView[329:907] end: 1 / coordinates count: 85
2013-02-21 10:31:22.541 SIGView[329:907] end: 2 / coordinates count: 7
2013-02-21 10:31:22.542 SIGView[329:907] end: 2 / coordinates count: 68
2013-02-21 10:31:22.650 SIGView[329:907] end: 2 / coordinates count: 85
2013-02-21 10:31:22.653 SIGView[329:907] end: 3 / coordinates count: 7
2013-02-21 10:31:22.655 SIGView[329:907] end: 3 / coordinates count: 68
2013-02-21 10:31:22.796 SIGView[329:907] end: 3 / coordinates count: 85

我希望您了解问题并帮助解决此问题。

提前致谢!

编辑

以下是该方法的调用方式:

NSArray *argumentsToOverlay = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],locationsToLoadRegion, nil];

[self addOverlaysFromPointsWithStartFromAndPoints:argumentsToOverlay];

locationsToLoadRegion当数组分别包含 7、68 和 85 个对象时,这部分代码被调用了 3 次。

问题开始performSelector:的时间是第一次调用。

编辑

- (void)createPathWithCoordinates:(NSMutableArray *)coordinates{

    int count;

// This for run through all sub routes
    for (NSArray *trackings in coordinates) {
        [locationsToLoadRegion removeAllObjects];

        count = [trackings count];

        if (count > 1){

            // This for save the coordinates available for sub route
            for (NSInteger index = 0; index < count; index++)
            {
                Location *tempLocation = [trackings objectAtIndex:index];
                [locationsToLoadRegion addObject:tempLocation];
            }

            NSArray *argumentsToOverlay = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],locationsToLoadRegion, nil];

            [self addOverlaysFromPointsWithStartFromAndPoints:argumentsToOverlay];

        }

    }
}
4

2 回答 2

0
    NSArray *parameters = [NSArray arrayWithObjects:...];
    [self performSelector:... withObject:parameters ...];

您创建一个自动释放的数组(参数),它将在下一次调用函数之前被销毁。您仍然可以获得可用数据而不是异常/崩溃,这纯属巧合。

例如,一种解决方案是将参数保留为调用类的属性。

于 2013-02-21T14:07:22.367 回答
0

试试这个:

NSArray *argumentsToOverlay = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:1],
    [[locationsToLoadRegion copy] autorelease],
    nil];
于 2013-02-21T14:38:59.063 回答