Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Created December 14, 2012 10:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swissmanu/4284339 to your computer and use it in GitHub Desktop.
Save swissmanu/4284339 to your computer and use it in GitHub Desktop.
Draws a smiley face under iOS. Pass a mood value ranging from 0 to 1 for making it happy (or sad :( )
/**
* Draws a smiley inside the given rectangle. The mood value (0...1) is used
* to make the smiley happy or sad.
*
* @param rect Rectangle to draw the smiley into
* @param mood Happy (= 1) or sad (= 0) or anything in between
*/
-(void)drawSmileyInRect:(CGRect)rect withMood:(float)mood {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, rect.origin.x, rect.origin.y);
CGContextScaleCTM(ctx, rect.size.width/8 ,rect.size.height/8); // Hint: Original Coordinate System has a width an height of 8 pixels
// Eyes:
CGContextFillEllipseInRect(ctx, CGRectMake(2, 2, 1, 1));
CGContextFillEllipseInRect(ctx, CGRectMake(5, 2, 1, 1));
// Lips:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(1.5, 5.5)];
[path addQuadCurveToPoint:CGPointMake(6.5, 5.5) controlPoint:CGPointMake(4, 4+mood*4)];
path.lineWidth = 0.5;
path.lineCapStyle = kCGLineCapRound;
[[UIColor blackColor] setStroke];
[path stroke];
CGContextRestoreGState(ctx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment