Skip to content

Instantly share code, notes, and snippets.

@aalemayhu
Forked from swissmanu/gist:4284339
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalemayhu/4d4fea357521b257ccc1 to your computer and use it in GitHub Desktop.
Save aalemayhu/4d4fea357521b257ccc1 to your computer and use it in GitHub Desktop.
/**
* 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