iOS 6 - Mapview,叠加淡入

时间:2013-01-24 09:38:41

标签: iphone ios ios5 ios6 overlay

我一直在构建一个应用程序,用于绘制当前用户沿着地图移动的线条。现在,在iOS5中,当我将叠加层添加到mapview时,它只是直接添加它,在iOS6中它添加了带有“淡入”动画的叠加层。我不想要这个烦人的动画。我还有一个功能,可以在保存的坐标上播放(在用户完成跑步或走路之后),当我播放时,线条闪烁非常快,仅仅是因为这个动画。这真的很烦人,我真的很感激任何指导或解决方案来摆脱这个动画。

添加叠加层的代码:

MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D)*2);
pointsArray[0]= MKMapPointForCoordinate(oldLocation.coordinate); 
pointsArray[1]= MKMapPointForCoordinate(newLocation.coordinate);
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
free(pointsArray);    
[self.mapView addOverlay:self.routeLine];

显示叠加的代码:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
    MKOverlayView *overlayView = nil;
    MKPolylineView  *routeLineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
    routeLineView.fillColor = [UIColor blueColor];
    routeLineView.strokeColor = [UIColor blueColor];
    routeLineView.backgroundColor = [UIColor clearColor];
    routeLineView.lineWidth = 10;
    routeLineView.lineCap = kCGLineCapRound;
    overlayView = routeLineView;
    return overlayView;
}

谢谢!

编辑:

通过路径的代码:

playTimer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                 target:self
                                               selector:@selector(playPathTimer)
                                               userInfo:nil
                                                repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:playTimer forMode:NSRunLoopCommonModes];

- (void)playPathTimer{
    MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D) * 2);
    double latitude1 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter]).latitude.doubleValue;
    double longitude1 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter]).longitude.doubleValue;
    double latitude2 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter+nextCord]).latitude.doubleValue;
    double longitude2 = ((WorldPoint *)[coordsArray objectAtIndex:playCounter+nextCord]).longitude.doubleValue;
    CLLocation *location1 = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1)
                                                          altitude:0
                                                horizontalAccuracy:0
                                                  verticalAccuracy:0
                                                         timestamp:[NSDate date]];
    CLLocation *location2 = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude2, longitude2)
                                                          altitude:0
                                                horizontalAccuracy:0
                                                  verticalAccuracy:0
                                                         timestamp:[NSDate date]];
    pointsArray[0] = MKMapPointForCoordinate(location1.coordinate);
    pointsArray[1] = MKMapPointForCoordinate(location2.coordinate);
    self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

    [self.mapView addOverlay:self.routeLine];
    [playRouteLines addObject:self.routeLine];


//    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(latitude1, longitude1);

    playCounter = playCounter + nextCord;
    if(playCounter + nextCord >= coordsArray.count){
//        [displayLink setPaused:YES];
//        [displayLink invalidate];
       [playTimer invalidate];
        playTimer = nil;
        playCounter = 0;
        [self showStartAndEndFlags];
        if(ee.trail.checkpoint.count > 0){
            [self showCheckpoints];
        }
        self.playButton.enabled = YES;
        MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:[self getRegionWithCorrectZooming]];
        [self.mapView setRegion:adjustedRegion animated:YES];
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用自己的MKOverlayView子类(MKPolylineView的超类),然后覆盖drawMapRect:zoomScale:inContext:您可以自己绘制线条,而不是使用MKPolylineView。

我猜这个你不会有动画淡化(我正在使用它并且从未注意到任何动画)。

为drawMapRect尝试以下代码:zoomScale:inContext:implementation。 您需要创建属性lineColor(CGColor)和lineWidth(CGFloat)。

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    MKPolyline *polyline = self.overlay;
    CGContextSetStrokeColorWithColor(context, self.lineColor);
    CGContextSetLineWidth(context, self.lineWidth/zoomScale);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint *points = malloc(sizeof(CGPoint)*polyline.pointCount);
    for (int i=0; i<polyline.pointCount; i++) {
        points[i] = [self pointForMapPoint:polyline.points[i]];
    }
    CGPathAddLines(path, NULL, points, polyline.pointCount);

    CGContextAddPath(context, path);

    CGContextDrawPath(context, kCGPathStroke);
    CGPathRelease(path);
    free(points);
}