Wednesday, June 5, 2013

UIBezierPath flatness example in Objective C (iOS).

UIBezierPath flatness

The factor that determines the rendering accuracy for curved path segments.

@property(nonatomic) CGFloat flatness

Discussion of [UIBezierPath flatness]
The flatness value measures the largest permissible distance (measured in pixels) between a point on the true curve and a point on the rendered curve. Smaller values result in smoother curves but require more computation time. Larger values result in more jagged curves but are rendered much faster. The default flatness value is 0.6.

In most cases, you should not change the flatness value. However, you might increase the flatness value temporarily to minimize the amount of time it takes to draw a shape temporarily (such as during scrolling).

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

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


Example of [UIBezierPath flatness].
myPath=[[UIBezierPath alloc]init];
myPath.lineJoinStyle = kCGLineJoinBevel;
myPath.flatness = 0.4;

UIBezierPath flatness example.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        [self.spooleteView removeHandView];
    UITouch *touch = [touches anyObject];
    lastPoint=[touch locationInView:self];

    //UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [currentPath moveToPoint:p];
    currentPath.flatness = 10.0;

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    UIBezierPath *myPath=[[UIBezierPath alloc]init];

    [dict setObject:myPath forKey:@"Path"];
    colorTag= [[AppHelper userDefaultsForKey:@"ColorTag"]intValue];

    self.selectedColor = [brushColor objectAtIndex:colorTag];

        self.selectedColor = [UIColor whiteColor];

        if([mode isEqualToString:@"morsecode"])

    //[dict setObject:[brushColor objectAtIndex:colorTag] forKey:@"Colors"];
    [dict setObject:self.selectedColor forKey:@"Colors"];

    [myArr addObject:dict];
    currentPath.flatness = 0.1;
    currentPath.lineJoinStyle = kCGLineJoinRound;
    [myPath moveToPoint:lastPoint];
    [myPath release];
    [dict release];   

End of UIBezierPath flatness example article.