OGeek|极客世界-中国程序员成长平台

标题: iphone - ios中的手绘折线叠加 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:55
标题: iphone - ios中的手绘折线叠加

我正在尝试使用叠加层(MKOverlay)在 MKMapView 上徒手追踪路线。

每次我们移动手指时,我都会用新坐标扩展最后一个坐标的折线,除了扩展折线覆盖时,整个覆盖都在设备中闪烁(仅有时),所以我无法追踪问题.

我尝试过的代码如下。

- (void)viewDidLoad

{

    j=0;    
    coords1 = malloc(2* sizeof(CLLocationCoordinate2D));

    coordinatearray=[[NSMutableArray alloc]init];

    UIPanGestureRecognizer *GestureRecogonized = [[UIPanGestureRecognizer alloc]     initWithTarget:self actionselector(gestureDetacted];

    [self.myMapView addGestureRecognizer:GestureRecogonized];

}

- (void)gestureDetactedUIPanGestureRecognizer *)recognizer
{

    if(UIGestureRecognizerStateBegan==recognizer.state)
    {

        CGPoint point = [recognizer locationInView:self.myMapView];   
        CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];

        CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:tapPoint.latitude longitude:tapPoint.longitude];

        [coordinatearray addObject:curLocation];
    }

    coords1[0]=[[coordinatearray objectAtIndex:j] coordinate];

    if(UIGestureRecognizerStateChanged==recognizer.state)  
    {
         j++;

         CGPoint point = [recognizer locationInView:self.myMapView];
         CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];

        CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:tapPoint.latitude longitude:tapPoint.longitude];

        [coordinatearray addObject:curLocation];
        coords1[1]=CLLocationCoordinate2DMake(tapPoint.latitude,tapPoint.longitude);

        polyLine = [MKPolyline polylineWithCoordinates:coords1 count:2];

        [self.myMapView addOverlay:polyLine];
    }
}

在覆盖委托(delegate)中

- (MKOverlayView *)mapViewMKMapView *)mapView viewForOverlayid <MKOverlay>)overlay {

    if([overlay isKindOfClass:[MKPolyline class]]){

        MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolylineverlay];

        polylineView.strokeColor = [UIColor orangeColor];
        polylineView.lineWidth = 20;

        polylineView.fillColor=[[UIColor orangeColor] colorWithAlphaComponent:.1];

         return polylineView;
    }
}

谁能知道为什么会出现这种闪烁或闪烁效果以及如何消除它。

提前致谢。



Best Answer-推荐答案


与其添加数百个非常小的 View (这确实是计算密集型的),我宁愿删除折线叠加层并在平移识别器的每次更改时添加一个包含 map 上所有点的新 View (对于更平滑的效果,可以先添加新的,然后删除旧的)。使用您的 coordinateArray 创建包含所有点的 MKPolyline 叠加层,而不是最后两个点。 你可以这样做:

[coordinatearray addObject:curLocation];;
CLLocationCoordinate2D* coordArray = malloc(sizeof(CLLocationCoordinate2D)*[coordinatearray count]);
memcpy(coordArray, self.coordinates, sizeof(CLLocationCoordinate2D)*([coordinatearray count]-1));    
coordArray[[coordinatearray count]-1] = curLocation;
free(self.coordinates);
self.coordinates = coordArray;
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordArray count:[coordinatearray count]];
MKPolyline *old = [[self.mapView overlays] lastObject];
[self.mapView addOverlay:polyline];
[self.mapView removeOverlayld];

其中 self.coordinate 是 CLLocationCoordinate2D* 类型的属性,这样你就可以将现有的数组 memcpy 到一个新的数组中(memcpy 真的很有效)并且只需要将最后一个点添加到数组中,而不必循环通过 NSArray *coordinatearray 的每个点。

您还必须更改您的 if(UIGestureRecognizerStateBegan==recognizer.state) 部分,以在 self.coordinates 中添加第一个点击点。 就像这样:

self.coordinates = malloc(sizeof(CLLocationCoordinate2D));
self.coordinates[0] = curLocation;

编辑:我认为问题在于 map 叠加层为缩放级别的不同离散值绘制自己。在某个缩放级别上,似乎覆盖首先以较大的缩放级别绘制自身,然后以较小的缩放级别绘制(实际上是在先前绘制的覆盖上绘制)。当您尝试在此缩放级别显示诸如绘制用户平移手势的动画时,这就是叠加层不断闪烁的原因。一种可能的解决方案是使用放置在 map View 顶部的透明 View ,在用户不断移动手指的同时执行绘图。只有在平移手势结束后,您才会创建一个 map 叠加层,将其“固定”到 map 上并清理绘图 View 。重绘 View 时还需要小心,因为每次都应仅指定要重绘的矩形,然后仅在该矩形中重绘,因为每次重绘整个事物也会导致该 View 闪烁。这肯定涉及更多的编码,但它应该可以工作。检查此question了解如何在 View 上逐步绘制。

关于iphone - ios中的手绘折线叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476824/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4