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.lineWidth=5;
    myPath.miterLimit=-10;
    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.lineCapStyle=kCGLineCapSquare;
myPath.lineJoinStyle = kCGLineJoinBevel;
myPath.lineWidth=5;
myPath.flatness = 0.4;

UIBezierPath flatness example.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(isFirstTapp)
    {
        isFirstTapp=NO;
        [self.spooleteView removeHandView];
    }
    isdraw=YES;
    mouseSwapped=NO;
    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];


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

    self.selectedColor = [brushColor objectAtIndex:colorTag];

    if(isErase)
    {
        myPath.lineWidth=30.0;
        self.selectedColor = [UIColor whiteColor];

    }
    else
    {
        if([mode isEqualToString:@"morsecode"])
            myPath.lineWidth=10.0;
        else
            myPath.lineWidth=5.0;
    }

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

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

End of UIBezierPath flatness example article.