Wednesday, June 5, 2013

UIBezierPath miterLimit example in Objective C (iOS).

UIBezierPath miterLimit

The limiting value that helps avoid spikes at junctions between connected line segments.

@property(nonatomic) CGFloat miterLimit

Discussion of [UIBezierPath miterLimit]
The miter limit helps you avoid spikes in paths that use the kCGLineJoinMiter join style. If the ratio of the miter length—that is, the diagonal length of the miter join—to the line thickness exceeds the miter limit, the joint is converted to a bevel join. The default miter limit is 10, which results in the conversion of miters whose angle at the joint is less than 11 degrees.

UIBezierPath miterLimit example.
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    myPath=[[UIBezierPath alloc]init];
    myPath.lineCapStyle = kCGLineCapRound;
    myPath.lineJoinStyle = kCGLineJoinMiter;
    myPath.flatness = 0.0;  

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath];


Example of [UIBezierPath miterLimit].
- (void)drawRect:(CGRect)rect
    [[UIColor redColor] setStroke];

    UIBezierPath *drawPath = [UIBezierPath bezierPath];

    drawPath.lineCapStyle = kCGLineCapRound;
    drawPath.miterLimit = 0;
    drawPath.lineWidth = 3.0;

    for (UIBezierPath *path in previousPaths)
        [drawPath appendPath:path];

    UIBezierPath *path = [self pathForCurrentLine];
    if (path)
        [drawPath appendPath:path];

    [drawPath stroke];

UIBezierPath miterLimit example.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.currentPath=[[UIBezierPath alloc]init];
    self.currentPath.lineCapStyle = kCGLineCapRound;
    self.currentPath.flatness = 0.0;

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [self.currentPath moveToPoint:[mytouch locationInView:self]];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:currentfColor forKey:@"fColor"];
    [dic setObject:currentsColor forKey:@"sColor"];
    [dic setObject:self.currentPath forKey:@"path"];
    [pathArray addObject:dic];


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self.currentPath addLineToPoint:[touch locationInView:self.view]];
    [self.view setNeedsDisplay];

End of UIBezierPath miterLimit example article.