2013-04-03 97 views
-1

我有油漆應用程序。當我點擊按鈕它啓動畫圖視圖比用戶可以畫。在我回來後,點擊按鈕,paintview將不會重置。如何在繪製疼痛之後重置按鈕中的視圖。繪製油漆後添加橡皮擦工具

代碼:

- (BOOL) initContext:(CGSize)size { 

    int bitmapByteCount; 
    int bitmapBytesPerRow; 

    // Declare the number of bytes per row. Each pixel in the bitmap in this 
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and 
    // alpha. 
    bitmapBytesPerRow = (size.width * 4); 
    bitmapByteCount = (bitmapBytesPerRow * size.height); 

    // Allocate memory for image data. This is the destination in memory 
    // where any drawing to the bitmap context will be rendered. 
    cacheBitmap = malloc(bitmapByteCount); 
    if (cacheBitmap == NULL){ 
     return NO; 
    } 
    cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst); 
    return YES; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [self drawToCache:touch]; 
    [self setNeedsDisplay]; 
} 

- (void) drawToCache:(UITouch*)touch { 
    hue += 0.005; 
    if(hue > 1.0) hue = 0.0; 
    UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0]; 

    CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]); 
    CGContextSetLineCap(cacheContext, kCGLineCapRound); 
    CGContextSetLineWidth(cacheContext, 15); 

    CGPoint lastPoint = [touch previousLocationInView:self]; 
    CGPoint newPoint = [touch locationInView:self]; 

    CGContextMoveToPoint(cacheContext, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(cacheContext, newPoint.x, newPoint.y); 
    CGContextStrokePath(cacheContext); 

    CGRect dirtyPoint1 = CGRectMake(lastPoint.x-10, lastPoint.y-10, 20, 20); 
    CGRect dirtyPoint2 = CGRectMake(newPoint.x-10, newPoint.y-10, 20, 20); 
    [self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)]; 
} 




// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGImageRef cacheImage = CGBitmapContextCreateImage(cacheContext); 
    CGContextDrawImage(context, self.bounds, cacheImage); 
    CGImageRelease(cacheImage); 


} 

在視圖控制器代碼:

PaintView *paint = [[PaintView alloc] initWithFrame:self.view.bounds]; 
    [self.view addSubview:paint]; 
    [paint release]; 
+0

最簡單的方法就是刪除視圖並重新創建它。 – Venkat

+0

添加一些代碼到你的問題,你的問題是不清楚的...重置你的看法取決於你寫了什麼代碼讓用戶在該視圖上繪製 –

回答

0

在觸摸移動,,, 作出橡皮擦的肘和更改代碼如下。

if(togleEraser == NO) 
{ 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 

    CGContextBeginPath(UIGraphicsGetCurrentContext()); 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x,   currentPoint.y); 
} 
else 
{ 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 20); 

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10); 

    CGContextBeginPath(UIGraphicsGetCurrentContext()); 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

    CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 50, 50)); 
} 
+0

當我在touchMoved中添加上面的代碼時,我得到了錯誤.. – Ram

+0

你能告訴我關於錯誤..? – aBilal17