Ranjit
Ranjit

Reputation: 4636

how to set different colors for UIBezeirPath by selecting color options

I am working with UIBezeirPath, and in my drawRect method, I have taken a hardcoded color to setStroke for my path(line) in the below way

- (void)drawRect:(CGRect)rect
{ 
 [[UIColor redColor] setFill];
 for (UIBezierPath *_path in pathArray) 
 {
      [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

  }
}

this works fine and set red color for the path(line) I draw. Now when I select diffrent color from color options, suppose I select blue color, now when I start to draw the path, the color is blue, but the previous drawn red line also changes to blue.and that is the whole issue

below is my code how I am setting different colors

- (void)drawRect:(CGRect)rect
{
if(changecolor)
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        strokeColor = appDelegate.color;
        NSLog(@"%@",strokeColor);

        SEL blackSel = NSSelectorFromString(strokeColor);
        UIColor* tColor = nil;
        if ([UIColor respondsToSelector: blackSel])
            tColor  = [UIColor performSelector:blackSel];
        [tColor setStroke];
        [tColor setFill];       
    }
    else
    {    
        [[UIColor redColor] setStroke];
        [[UIColor redColor] setFill];

        for (UIBezierPath *_path in pathArray) 
        {
            [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

        }
   }
}

Whether I am doing this right way, or am I missing something. please help me out

Regards Ranjit

Upvotes: 2

Views: 1251

Answers (1)

Veera Raj
Veera Raj

Reputation: 1562

Just maintain current color in one array and bezierpaths in another array. Then do the drawing like following.

    int q=0;

for (UIBezierPath *_path in pathArray) 
{
UIColor *_color = [colorArray objectAtIndex:q];
[_color setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
q++;
}

Upvotes: 5

Related Questions