Reputation: 4646
I created a class called myBezierPaths, with two member variables of type UIBezierPath and UIColor, and then I tried to use object of this class to draw the bezierpath, but I am not getting bezierpath, below is my code
//Bezierpath.h
@interface BezierPath : NSObject
{
UIBezierPath *m_bezierPath;
UIColor *m_pathColor;
}
@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (nonatomic, strong) UIColor *pathColor;
@end
//drawingView.m
- (void)drawRect:(CGRect)rect
{
for (BezierPath *pathobj in m_pathArray)
{
[pathobj.pathColor setStroke];
[pathobj.pathColor setFill];
[pathobj.bezierPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
*self.myPath = [[BezierPath alloc] init];*
myPath.bezierPath.lineWidth = 5;
myPath.bezierPath.lineCapStyle = kCGLineCapRound;
myPath.bezierPath.flatness = 0.0;
myPath.bezierPath.lineJoinStyle = kCGLineJoinRound;
myPath.bezierPath.miterLimit = 200.0;
self.myPath.pathColor = [UIColor redColor];
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[myPath.bezierPath moveToPoint:[mytouch locationInView:self]];
[m_pathArray addObject:self.myPath];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[self.myPath.bezierpath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
hope I am clear with my explanation, waiting for reply
Regards Ranjit
Upvotes: 2
Views: 2438
Reputation: 454
@Ranjit need some help I've same problem ... I want to change the colors of UIbezierpath. but wen i select the color my previous path also automatically gets colored with new color.
In My first Image i draw a line and selected color is red. but when i select yellow color to draw another line the previous line also change to yellow color.(image no 2)
Upvotes: 0
Reputation: 8502
First, in your drawRect
you don't actually fill the shape. You do stroke it (basically draw a border) but you need to call [pathObj.bezierPath fill];
to actually fill the shape with a color.
There's a lot of code you're not showing, so it's hard to tell where the problem might be. I would create a breakpoint in the drawRect
method to determine if all of the variables are there. For example, you show (what I assume) to be an array: m_pathArray
. Are you sure it's initialized? Are you sure the properties of BezierPath
are working correctly? I notice that on touchesBegan
you set the pathColor
but only access/read the bezierPath
variable. Has that been initialized? Where do you create the 'm_bezierPath' instance variable? Do you create it in the BezierPath init
method or lazily instantiate it on access?
So those are a few things you could check.
Upvotes: 2
Reputation: 17378
You don't show your touchesMoved
event handler , but i would expect something like this. Otherwise all you are doing is creating a set of paths of length zero.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
[self.myPath.bezeirPath lineToPoint:[mytouch locationInView:self]];
}
Upvotes: 0