Search code examples
iphonecgcontext

How would I draw this using CGContext


I want to draw this in my view to draw this line, I have everything I need to make a basic line, but am just not good at drawing, I did in fact try to do this, but just cannot get it to work correctly. alt text


Solution

  • The following code should draw a sine curve like the one you describe, assuming currentBounds is the bounding rectangle for your area to draw within:

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 0.0f, CGRectGetMidY(currentBounds));
    CGContextAddCurveToPoint(context, currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidX(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) - currentBounds.size.width / 5.0f, CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextAddCurveToPoint(context, CGRectGetMidX(currentBounds) + currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) + currentBounds.size.width / 5.0f, currentBounds.size.width - currentBounds.size.width / 5.0f, CGRectGetMidY(currentBounds) + currentBounds.size.width / 5.0f, currentBounds.size.width, CGRectGetMidY(currentBounds));
    CGContextClosePath(context);
    CGContextStrokePath(context);