2012-04-19 126 views
0

enter image description here我正在使用以下代碼在iphone中創建畫筆。如何在iphone中繪製畫筆時從線上刪除點?

@interface Canvas : UIImageView { 
    CGPoint location; 
} 

@property CGPoint location; 
.m file 
@synthesize location; 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    self.location = [touch locationInView:self];  
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentLocation = [touch locationInView:self]; 

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 


    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetBlendMode(ctx, kCGBlendModeNormal); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    //CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, location.x, location.y); 
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
    CGContextStrokePath(ctx); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    location = currentLocation; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentLocation = [touch locationInView:self]; 

    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetBlendMode(ctx, kCGBlendModeOverlay); 

    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
    CGContextSetBlendMode(ctx, kCGBlendModeNormal); 
    CGContextSetLineCap(ctx, kCGLineCapRound); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, location.x, location.y); 
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y); 
    CGContextStrokePath(ctx); 
    self.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    location = currentLocation;  
} 

它的工作,但同時提請有一定的距離後,一些點在該行正在drawn.i要刪除這些點,並希望順利直line.How我可以做到這一點?

回答

0

您使用相同的圖像一遍又一遍地乘以舊圖像,對吧? 我會嘗試在每次觸摸時使用新鮮的上下文。然後在現有圖像上乘以它。 也許這是奇怪的外觀的原因?

如果開始和結束點不同,也可以添加檢查。

而且你應該製作一個額外的繪圖方法。在TouchesMoved和TouchesEnded中有冗餘代碼。

+0

即使我改變模式以正常的效果come.that不是bcoz一次again.it使用同樣的圖像也談到第一次 – Bhoomi 2012-04-19 15:17:17

+0

告訴我們結果的截圖..也許沒有什麼幫助。 – calimarkus 2012-04-19 15:52:18

+0

所以只有在使用透明顏色繪製時纔會出現該問題?堅實的藍色線似乎是好的?您可以保存touchesmoved的所有位置,並在每次更改時重新繪製整個當前行。 – calimarkus 2012-04-20 19:54:16