4

我正在做一个绘图项目,我有一个橡皮擦选项。下面给出的代码适用于当我启动我的应用程序并绘制一些线条并继续使用橡皮擦时。它工作正常,我得到了橡皮擦效果。现在第二种情况是我画了大约 10 条线,然后单击“撤消按钮”并撤消整个事情,然后我重做整个事情,现在当我单击“橡皮擦按钮”并尝试擦除某些部分时,但相反,它将清除整个绘图。这是我想要弄清楚的,但我不明白我哪里出错了,所以朋友们,请帮助我。

下面是我的代码。

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

}

请告诉我我是否做得对。

问候,

兰吉特

4

2 回答 2

5

我已经解决了这个问题,问题和下面是完美运行的代码

case :ERASE
{
 CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
            CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);

            [m_curImage drawAtPoint:CGPointMake(0, 0)];
            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);


            CGContextSetBlendMode(context, kCGBlendModeClear);
            CGContextSetLineJoin(context, kCGLineJoinRound);
            CGContextSetLineWidth(context, self.lineWidth);
            CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
            CGContextSetShouldAntialias(context, YES);  
            CGContextSetAllowsAntialiasing(context, YES); 
            CGContextSetFlatness(context, 0.1f);

            CGContextSetAlpha(context, self.lineAlpha);
            CGContextStrokePath(context); 
}

问候兰吉特。

这解决了我的问题,如果有人有同样的问题,请执行此操作。

于 2012-07-23T11:01:56.100 回答
2

这里发生的事情是当你擦除整个矩形时会变得清晰 na?? 因此,不要使用该擦除方法以与屏幕相同的颜色绘制线条,以便看起来像窗口正在清除并且它也很容易尝试这个。

例如,如果您的绘图屏幕是黑色的,则在屏幕上以黑色绘制 touchesmoved 线,使其看起来像清除。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  lastPoint = [touch locationInView:imagevw];
  }

  -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   UITouch *touch = [touches anyObject];
   CGPoint currentPoint = [touch locationInView:imagevw];
   UIGraphicsBeginImageContext(self.imagevw.frame.size);

  [self.imagevw.image drawInRect:CGRectMake(0, 0, self.imagevw.frame.size.width,        self.imagevw.frame.size.height)];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); // black color
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
     imagevw.image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

这是在黑色视图上用黑色画线的代码......试试......它可能对你有帮助......

于 2012-07-18T07:44:21.987 回答