带有TouchesEnded的NSUndoManager用于MKPolyline

时间:2015-10-01 22:52:11

标签: objective-c mkmapview mkoverlay nsundomanager mkpolyline

这是我第一次尝试使用NSUndoManager,我不确定我应该把它放在哪里/我错过了什么。我正在创建一个具有带有绘图按钮和撤消按钮的地图视图的应用程序。我有绘图按钮使用户能够绘制折线,每当他们的手指离开屏幕时,它将另一条线连接到他们刚绘制的线。我希望撤消按钮摆脱他们绘制的最后一行。

到目前为止我的一些内容......

- (void)viewDidLoad
{
[super viewDidLoad];
...
...
undoManager = [[NSUndoManager alloc]init];
}

- (IBAction)oopsButton:(id)sender {

[undoManager undo];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
....
....
[[undoManager prepareWithInvocationTarget:self]touchesEnded:(touches) withEvent:event];
[undoManager setActionName:NSLocalizedString(@"Route Changed", @"route undo")];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay

...
...

现在,当我点击撤消时,它会撤消一行,但这就是全部。我需要它来保持撤消的次数与用户点击次数相同。感谢您对此提供的任何帮助,我一直在寻找谷歌和堆栈,并且找不到任何有用的东西。

1 个答案:

答案 0 :(得分:0)

我找到了一种更新折线而无需使用撤消管理器...

的方法
- (IBAction)oopsButton:(id)sender {

    //remove last point from array
[self.coordinates removeLastObject];

    //update line on map
NSInteger numberOfPoints = [self.coordinates count];

if (numberOfPoints > 1)
{
    MKPolyline *oldPolyLine = self.polyLine;
    CLLocationCoordinate2D points[numberOfPoints];
    for (NSInteger i = 0; i < numberOfPoints; i++)
        points[i] = [self.coordinates[i] MKCoordinateValue];
    MKPolyline *newPolyLine = [MKPolyline polylineWithCoordinates:points count:numberOfPoints];
    [self.mapView addOverlay:newPolyLine];

    self.polyLine = newPolyLine;

    if (oldPolyLine)
        [self.mapView removeOverlay:oldPolyLine];

}
}