0

这是我第一次尝试使用 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

...
...

现在,当我单击撤消时,它会撤消一行,仅此而已。我需要它在用户单击时保持撤消的次数。感谢您对此的任何帮助,我一直在寻找谷歌和堆栈,但还没有找到任何有帮助的东西。

4

1 回答 1

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];

}
}
于 2015-10-02T22:43:26.570 回答