How to draw UIBezierPaths

Here is what I want to do:

I have a UIBezierPath, and I want to pass it to some method to draw it. Or simply draw it from the method in which it is created.

I am not sure how to indicate what kind of view it should be drawn. Should all drawing methods start with

- (void)drawRect:(CGRect)rect { ...} ?

can i do

- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ??

How do I call this function or method from another method?

+5
source share
5 answers

drawRect:- This is what is called automatically when a message setNeedsDisplayor setNeedsDisplayInRect:in a view. You never call drawRect:directly.

However, you are correct that all drawing operations are performed within the framework of the method drawRect:. Typical implementation:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    /* Do your drawing on `context` */
}

UIBezierPath s, , , setNeedsDisplay, - .

- (void)drawRect:(CGRect)rect {    
    for ( UIBezierPath * path in bezierPaths ) {
        /* set stroke color and fill color for the path */
        [path fill];
        [path stroke];
    }
}

bezierPaths - UIBezierPath s.

+20

ivar

@interface SomeView {
  UIBezierPath * bezierPath;
}
@property(nonatomic,retain) UIBezierPath * bezierPath;
...
@end
....
- (void)someMethod {
     self.bezierPath = yourBezierPath;
     [self setNeedsDisplayInRect:rectToRedraw];
}

-drawRect:

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 3.0);
    CGContextSetLineCap(currentContext, kCGLineCapRound);
    CGContextSetLineJoin(currentContext, kCGLineJoinRound);
    CGContextBeginPath(currentContext);
    CGContextAddPath(currentContext, bezierPath.CGPath);
    CGContextDrawPath(currentContext, kCGPathStroke);
}
+6

, -drawRect: :

- (void)drawRect:(CGRect)rect
{
  // config your context
  [bezierPath stroke];
}

: -stroke .

+3

-drawRect: ( , setNeedsDisplay). , drawRect:withBezierPath: . , , - .

If you have it UIBezierPath, it is very easy to draw it:

- (void)drawRect:(CGRect)rect {
  UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar?
  [path stroke];
}

There is no need to work with Core Graphics if all you want to do is draw a path.

+1
source

you can do something like the following. just define UIColor * setStroke; in the .h file and you need to set this strokeColor object before you call[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

 - (void)drawRect:(CGRect)rect
    {
        [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        for(UIBezierPath *_path in pathArray)
            [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }
+1
source

All Articles