我正在尝试向iOS应用添加撤消/重做功能。我希望能够画几条线,然后每条线都撤消。.我可以擦除整个内容,但这还不够好。我真的很感谢帮助,因为这是我第一次尝试使用CG。
我的.h声明包括:
CGPoint lastPoint;
NSMutableArray *pathArray;
UIBezierPath *myPath;
在.m中,我有:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%s", __FUNCTION__);
UITouch *touch = [touches anyObject];
myPath=[[UIBezierPath alloc]init];
lastPoint = [touch locationInView:self.view];
[myPath moveToPoint:lastPoint];
lastPoint.y -= 20;
[pathArray addObject:myPath];
NSLog(@"pathArray count is %i", [pathArray count]);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%s", __FUNCTION__);
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
savedImage.image = drawImage.image;
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
在touchesBegin的末尾,pathArray的计数始终为零。
要实现撤消,我使用以下代码:
- (void)undoButtonTapped {
NSLog(@"%s", __FUNCTION__);
NSLog(@"pathArray count is %i", [pathArray count]);
if([pathArray count]>0){
UIBezierPath *_path=[pathArray lastObject];
[bufferArray addObject:_path];
[pathArray removeLastObject];
[self.view setNeedsDisplay];
}
}
这里的计数也为零。
所有这些都在UIViewController中处理。我欢迎任何建议/改进/建议。
谢谢
最佳答案
我想说[pathArray count]
在touchesBegan
中为零的原因是我的代码中没有地方
pathArray = [[NSMutableArray alloc] init];
因此,您正在将消息发送到空指针(允许,但不执行任何操作)。
那么您要分配pathArray吗?还是它为空?
关于ios - xCode-帮助尝试向UIBezierPath添加撤消/重做功能的帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10126828/