我正在繪製項目,我有一個橡皮擦選項。下面給出的代碼用於當我啓動我的應用程序並繪製一些行並繼續使用橡皮擦。它工作正常,我得到橡皮擦效果。現在第二個場景是我畫了10條線,然後點擊「撤消按鈕」並撤消整個事情,然後我重做整個事情,現在當我點擊「橡皮擦按鈕」並嘗試擦除某些部分,但相反,它會清除整個圖紙。這是我想弄清楚的,但我不明白我哪裏出了問題,所以請朋友幫幫我。橡皮擦不適用於iOS繪圖
以下是我的代碼。
- (void)drawRect:(CGRect)rect
{
case DRAW:
{
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetAlpha(context, self.lineAlpha);
CGContextSetAllowsAntialiasing(context, YES);
CGContextStrokePath(context);
// [super drawRect:rect];
}
break;
case ERASE:
{
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetBlendMode(context, kCGBlendModeClear);
[super drawRect:rect];
break;
}
case UNDO:
{
[m_curImage drawInRect:self.bounds];
break;
}
case REDO:
{
[m_curImage drawInRect:self.bounds];
break;
}
default:
break;
}
}
這些是當我點擊撤銷/重做時運行的函數。
-(void)redrawLine
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
NSDictionary *lineInfo = [m_lineArray lastObject];
m_curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:self.bounds];
}
-(void)undoButtonClicked
{
if([m_lineArray count] > 0)
{
NSMutableArray *line = [m_lineArray lastObject];
[m_bufferArray addObject:line];
[m_lineArray removeLastObject];
[self redrawLine];
}
m_drawStep = UNDO;
}
-(void)redoButtonClicked
{
if([m_bufferArray count] > 0)
{
NSMutableArray *line = [m_bufferArray lastObject];
[m_lineArray addObject:line];
[m_bufferArray removeLastObject];
[self redrawLine];
}
m_drawStep = REDO;
}
請告訴我我是否做得對。
問候,
蘭吉特
我也在研究相同的功能,但這個擦除不適合我,你能幫我嗎。當我在擦除模式下它不畫任何東西沒有清除任何東西..請幫助我 – Raj 2012-12-01 16:18:18
對不起。當我在擦除模式下其繪圖正常,沒有清除任何東西..請幫助我 – Raj 2012-12-01 16:28:40
我修正了這個問題,需要設置圖形的背景色來清除顏色..謝謝你很多解決方法 – Raj 2012-12-01 18:12:16