//
// YellowMan.swift
// 小黄人
//
// Created by admin on 16/1/23.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class YellowMan: UIView {
override func drawRect(rect: CGRect) {
let yellowMan = UIGraphicsGetCurrentContext()
let viewSize = self.frame.size
let middlePoint = CGPointMake(viewSize.width / 2, 0)
let yellowManW = viewSize.width * 0.5
let margin:CGFloat = 30
// 头部圆
let headerCircle = CGRectMake((viewSize.width - yellowManW) / 2, margin, yellowManW, yellowManW)
CGContextSetRGBFillColor(yellowMan, 253/255, 215/255, 62/255, 1)
CGContextAddEllipseInRect(yellowMan, headerCircle)
// 身体矩形
let body = CGRectMake(headerCircle.origin.x, margin + (yellowManW * 0.5) , yellowManW, yellowManW)
CGContextAddRect(yellowMan, body)
// 尾部圆
let footerCircel = CGRectMake((viewSize.width - yellowManW) / 2, yellowManW + margin, yellowManW, yellowManW)
CGContextClosePath(yellowMan)
CGContextAddEllipseInRect(yellowMan, footerCircel)
CGContextFillPath(yellowMan)
// 画眼镜
let glasserMargin:CGFloat = 5
let glasses = CGRectMake(body.origin.x - glasserMargin, body.origin.y + glasserMargin, yellowManW + 2 * glasserMargin, yellowManW * 0.15)
CGContextSetRGBFillColor(yellowMan, 2/255, 2/55, 6/255, 1)
CGContextAddRect(yellowMan, glasses)
CGContextFillPath(yellowMan)
// 画左眼睛
let eyeW = yellowManW * 0.8 * 0.5
let eyeMagin = (yellowManW - (eyeW * 2)) / 2
let leftEye = CGRectMake(body.origin.x + eyeMagin, glasses.origin.y - ((eyeW - glasses.size.height) / 2), eyeW, eyeW)
CGContextSetRGBFillColor(yellowMan, 58/255, 58/255, 58/255, 1)
CGContextAddEllipseInRect(yellowMan, leftEye)
// 画右眼睛
let rightEye = CGRectMake(body.origin.x + eyeMagin + eyeW, glasses.origin.y - ((eyeW - glasses.size.height) / 2), eyeW, eyeW)
CGContextAddEllipseInRect(yellowMan, rightEye)
CGContextFillPath(yellowMan)
// 画左眼睛
let eyeWa = eyeW * 0.8
let eyeMagina = ((eyeW - eyeWa) * 0.5)
let leftEyea = CGRectMake(leftEye.origin.x + eyeMagina, leftEye.origin.y + eyeMagina, eyeWa, eyeWa)
CGContextSetRGBFillColor(yellowMan, 254/255, 254/255, 254/255, 1)
CGContextAddEllipseInRect(yellowMan, leftEyea)
// 画右眼睛
let rightEyea = CGRectMake(rightEye.origin.x + eyeMagina, rightEye.origin.y + eyeMagina, eyeWa, eyeWa)
CGContextAddEllipseInRect(yellowMan, rightEyea)
CGContextFillPath(yellowMan)
// 画左眼睛
let eyeWb = eyeWa * 0.5 * 0.8
let eyeMaginb = eyeWa * 0.5
let leftEyeb = CGRectMake(leftEyea.origin.x + eyeMaginb, (leftEyea.size.height - eyeWb) * 0.5 + leftEyea.origin.y, eyeWb, eyeWb)
CGContextSetRGBFillColor(yellowMan, 114/255, 28/255, 26/255, 1)
CGContextAddEllipseInRect(yellowMan, leftEyeb)
// 画右眼睛
let rightEyeb = CGRectMake(rightEyea.origin.x + (eyeMaginb - eyeWb), (rightEyea.size.height - eyeWb) * 0.5 + rightEyea.origin.y, eyeWb, eyeWb)
CGContextAddEllipseInRect(yellowMan, rightEyeb)
CGContextFillPath(yellowMan)
// 画左眼睛
let eyeWc = eyeWb * 0.5
let eyeMaginc = (eyeWb - eyeWc) * 0.5
let leftEyec = CGRectMake(leftEyeb.origin.x + eyeMaginc, leftEyeb.origin.y + eyeMaginc, eyeWc, eyeWc)
CGContextSetRGBFillColor(yellowMan, 0/255, 0/255, 0/255, 1)
CGContextAddEllipseInRect(yellowMan, leftEyec)
// 画右眼睛
let rightEyec = CGRectMake(rightEyeb.origin.x + eyeMaginc, rightEyeb.origin.y + eyeMaginc, eyeWc, eyeWc)
CGContextAddEllipseInRect(yellowMan, rightEyec)
CGContextFillPath(yellowMan)
// 画左眼睛
let eyeWd = eyeWc * 0.5
let eyeMagind = (eyeWc - eyeWd) * 0.5
let leftEyed = CGRectMake(leftEyec.origin.x, leftEyec.origin.y, eyeWd, eyeWd)
CGContextSetRGBFillColor(yellowMan, 255/255, 255/255, 255/255, 1)
CGContextAddEllipseInRect(yellowMan, leftEyed)
// 画右眼睛
let rightEyed = CGRectMake(rightEyec.origin.x, rightEyec.origin.y, eyeWd, eyeWd)
CGContextAddEllipseInRect(yellowMan, rightEyed)
CGContextFillPath(yellowMan)
// 画笑脸
let smileRadius = yellowManW * 0.3
let startAngle:Double = 0.3
CGContextSetLineWidth(yellowMan, 2)
CGContextAddArc(yellowMan, middlePoint.x, CGRectGetMaxY(leftEye), smileRadius, CGFloat(startAngle * M_PI), CGFloat((1 - startAngle) * M_PI), 0)
CGContextSetRGBFillColor(yellowMan, 0/255, 0/255, 0/255, 0)
CGContextStrokePath(yellowMan)
}
}