2012-04-21 78 views
1

我正在使用以下代碼使用UIBezierPath爲圖像着色。有顏色選擇器,用戶可以從中選擇顏色。隨着顏色的不斷變化,將會在圖像上繪製更多的路徑。但是,當用戶在圖像中的某個位置填充顏色後,從顏色選擇器中選擇顏色並再次在同一位置填充顏色。在這種情況下,以前繪製的顏色在那裏。我想刪除以前填充的顏色並希望填充新的顏色。這隻在用戶在同一位置填充顏色時纔有效。在這裏我已經得到UIBezierPath已經繪製的點。有一種方法UIBezierPath removeAllPoints。但它會從路徑中移除所有點。我只想從uibezierpath中刪除只有觸及的點。我怎樣才能做到這一點?如何刪除或覆蓋iPhone中的觸摸點bezierpath?

#import "MyLineDrawingView.h" 


@implementation MyLineDrawingView 
@synthesize selImage; 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 

     arrBezierPaths=[[NSMutableArray alloc]init ]; 
     globalPath=[[UIBezierPath alloc]init]; 

     myPath=[[UIBezierPath alloc]init]; 

     self.userInteractionEnabled = TRUE; 

     myPath.lineWidth=30; 
     brushPattern=[UIColor redColor]; 
     NSLog(@"initWithFrame method called"); 
     NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:myPath,@"Path",brushPattern,@"Color", nil]; 
     [arrBezierPaths addObject:dict]; 

    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect 
{ 

    for (int i=0; i<[arrBezierPaths count]; i++) 
    { 
     NSDictionary *dict=[arrBezierPaths objectAtIndex:i]; 
     UIColor *tempBrushpatter=[[UIColor alloc]init ]; 
     tempBrushpatter=[dict valueForKey:@"Color"]; 

     globalPath=[dict valueForKey:@"Path"]; 

     [globalPath strokeWithBlendMode:kCGBlendModeOverlay alpha:1.0]; 
     [tempBrushpatter setStroke]; 

    } 

    } 

#pragma mark - Touch Methods 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 

    [globalPath moveToPoint:[mytouch locationInView:self]]; 

} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     CGPoint pointTouched = [mytouch locationInView:self]; 

for(int i=0;i<[arrBezierPaths count];i++) 
{ 
    NSDictionary *dictTemp=[arrBezierPaths objectAtIndex:0]; 
    UIBezierPath *temppath=[dictTemp valueForKey:@"Path"]; 
    if([temppath containsPoint:pointTouched]) 
    { 

     // [tempPath removeAllPoints]; 
     NSLog(@"This point already contain some path"); 
    } 
} 
    [globalPath addLineToPoint:[mytouch locationInView:self]]; 

[self setNeedsDisplay]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 
-(void)changeColor:(UIColor *)color 
{ 
    UIBezierPath *temp=[[UIBezierPath alloc]init]; 

    // self.userInteractionEnabled = TRUE; 

    temp.lineWidth=30; 
    UIColor *brushColor=color; 

    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:temp,@"Path",brushColor,@"Color", nil]; 
    [temp release]; 
    [arrBezierPaths addObject:dict]; 
    brushPattern=[color retain]; 
    [self setNeedsDisplay]; 
} 

回答

0

需要一些更多的細節。根據我的假設,您正在繪製所需顏色的線條,但下一次您只是想用另一種顏色替換觸點,而不是整條線?