ios - 在 iOS 中撤消/重做绘图
<p><p>我正在开发一个绘图应用程序,我想做撤消/重做,为此我将触摸结束时的 CGPath 保存到 NSMutableArray,但我不明白如何在单击撤消按钮时呈现 CGPaths</p >
<p>编辑1:</p>
<p>由于我使用的是 BezierPaths,所以,我首先决定采用一种简单的方法,即在不使用 CGPath 的情况下抚摸这条路径,</p>
<p>EDIT2:由于我的撤消是在分段中进行的(即,部分而不是整个路径被删除),我决定创建一个数组数组,所以我进行了相应的更改,现在我将在 CGlayer 中绘制,用CGPath</p>
<p>所以这里的“parentUndoArray”是数组数组。</p>
<p>所以我就这样做了</p>
<p>我有一个名为 DrawingPath 的类来进行绘图</p>
<pre><code>//DrawingPath.h
@interface DrawingPath : NSObject
@property (strong, nonatomic) NSString *pathWidth;
@property (strong,nonatomic) UIColor *pathColor;
@property (strong,nonatomic) UIBezierPath *path;
- (void)draw;
@end
//DrawingPath.m
#import "DrawingPath.h"
@implementation DrawingPath
@synthesize pathWidth = _pathWidth;
@synthesize pathColor = _pathColor;
@synthesize path = _path;
- (id)init {
if (!(self = ))
return nil;
_path = [ init];
_path.lineCapStyle=kCGLineCapRound;
_path.lineJoinStyle=kCGLineJoinRound;
;
return self;
}
- (void)draw
{
;
;
}
</code></pre>
<p>所以现在在我的 DrawingView 中,我是这样做的</p>
<pre><code>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
ctr = 0;
bufIdx = 0;
UITouch *touch = ;
pts = ;
isFirstTouchPoint = YES;
;//On every touches began clear undoArray
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = ;
CGPoint p = ;
ctr++;
pts = p;
if (ctr == 4)
{
pts = midPoint(pts, pts);
for ( int i = 0; i < 4; i++)
{
pointsBuffer = pts;
}
bufIdx += 4;
dispatch_async(drawingQueue, ^{
self.currentPath = [ init];
;
if (bufIdx == 0) return;
LineSegment ls;
for ( int i = 0; i < bufIdx; i += 4)
{
if (isFirstTouchPoint) // ................. (3)
{
ls = (LineSegment){pointsBuffer, pointsBuffer};
.firstPoint];
// .firstPoint];
isFirstTouchPoint = NO;
}
else
{
ls = lastSegmentOfPrev;
}
float frac1 = self.lineWidth/clamp(len_sq(pointsBuffer, pointsBuffer), LOWER, UPPER); // ................. (4)
float frac2 = self.lineWidth/clamp(len_sq(pointsBuffer, pointsBuffer), LOWER, UPPER);
float frac3 = self.lineWidth/clamp(len_sq(pointsBuffer, pointsBuffer), LOWER, UPPER);
ls = , pointsBuffer} ofRelativeLength:frac1]; // ................. (5)
ls = , pointsBuffer} ofRelativeLength:frac2];
ls = , pointsBuffer} ofRelativeLength:frac3];
.firstPoint]; // ................. (6)
.firstPoint controlPoint1:ls.firstPoint controlPoint2:ls.firstPoint];
.secondPoint];
.secondPoint controlPoint1:ls.secondPoint controlPoint2:ls.secondPoint];
;
lastSegmentOfPrev = ls; // ................. (7)
}
;
EDIT:2
CGPathRef cgPath = self.currentPath.path.CGPath;
mutablePath = CGPathCreateMutableCopy(cgPath);
dispatch_async(dispatch_get_main_queue(), ^{
bufIdx = 0;
;
});
});
pts = pts;
pts = pts;
ctr = 1;
}
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
;
}
</code></pre>
<p>下面是我的drawRect方法</p>
<p>编辑:现在我的 DrawRect 有两种情况</p>
<pre><code>- (void)drawRect:(CGRect)rect
{
switch (m_drawStep)
{
case DRAW:
{
CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw)
CGContextRef layerContext = CGLayerGetContext(self.currentDrawingLayer);
CGContextBeginPath(layerContext);
CGContextAddPath(layerContext, mutablePath);
CGContextSetStrokeColorWithColor(layerContext, self.lineColor.CGColor);
CGContextSetFillColorWithColor(layerContext, self.lineColor.CGColor);
CGContextSetBlendMode(layerContext,kCGBlendModeNormal);
CGContextDrawPath(layerContext, kCGPathFillStroke);
// CGPathRelease(mutablePath);
CGContextDrawLayerInRect(context,rectSize, self.newDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.permanentDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer );
}
break;
case UNDO:
{
for(int i = 0; i<;i++)
{
NSMutableArray *undoArray = ;
for(int i =0; i<;i++)
{
DrawingPath *drawPath = ;
;
}
}
}
break;
;
}
</code></pre>
<p>EDIT2:现在我面临的问题是,即使我绘制小路径或大路径,数组数组中的数据都是相同的。但实际上,小路径应该包含较少的drawingPath对象,大路径应该包含更多的undoArray中的drawingPath对象,最后添加到名为“ParentUndoArray”的数组数组中</p>
<p>这是屏幕截图,</p>
<p>1.第一屏不抬手指,一气呵成的画线截图</p>
<p> <img src="/image/ml1va.png" alt="enter image description here"/> </p>
<p>2、执行一次undo操作后,只删除该行的一部分</p>
<p> <img src="/image/UWoRO.png" alt="enter image description here"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经找到了解决方案,我们需要创建一个 <code>DrawingPaths</code> 的数组:</p>
<pre><code>- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Do the above code, then
;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
];
}
</code></pre>
<p>然后在<code>DrawRect</code>中描边路径。</p></p>
<p style="font-size: 20px;">关于ios - 在 iOS 中撤消/重做绘图,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21438586/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21438586/
</a>
</p>
页:
[1]