首页 > 代码库 > Quartz2D练习 -- 小黄人

Quartz2D练习 -- 小黄人

Main.storyboard

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="ufC-wZ-h7g">
            <objects>
                <viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
                        <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS" customClass="LWTView">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
            </objects>
        </scene>
    </scenes>
    <simulatedMetricsContainer key="defaultSimulatedMetrics">
        <simulatedStatusBarMetrics key="statusBar"/>
        <simulatedOrientationMetrics key="orientation"/>
        <simulatedScreenMetrics key="destination"/>
    </simulatedMetricsContainer>
</document>
View Code

LWTView.h

#import <UIKit/UIKit.h>

@interface LWTView : UIView

@end
View Code

LWTView.m

  1 //
  2 //  LWTView.m
  3 //  Quartz2D练习 -- 小黄人
  4 //
  5 //  Created by apple on 14-6-9.
  6 //  Copyright (c) 2014年 lwt. All rights reserved.
  7 //
  8 
  9 #import "LWTView.h"
 10 #define KTopX rect.size.width * 0.5
 11 #define KTopY 150
 12 #define KTopRadius 80
 13 
 14 @implementation LWTView
 15 
 16 
 17 // Only override drawRect: if you perform custom drawing.
 18 // An empty implementation adversely affects performance during animation.
 19 - (void)drawRect:(CGRect)rect
 20 {
 21     // Drawing code
 22     CGContextRef ctx = UIGraphicsGetCurrentContext();
 23     
 24     CGContextSaveGState(ctx);
 25     CGContextSaveGState(ctx);
 26     CGContextSaveGState(ctx);
 27     
 28     // 画身体
 29     [self drawBody:rect CGContextRef:ctx];
 30     
 31     // 1
 32     CGContextRestoreGState(ctx);
 33     
 34     // 画眼睛
 35     [self drawEyes:rect CGContextRef:ctx];
 36     
 37     // 3
 38     CGContextRestoreGState(ctx);
 39     
 40     //画嘴巴
 41     [self drawMouth:rect CGContextRef:ctx];
 42     
 43     // 画头发
 44     [self drawHair:rect CGContextRef:ctx];
 45 }
 46 
 47 - (void)drawMouth : (CGRect)rect CGContextRef : (CGContextRef)ctx
 48 {
 49     CGFloat controllerX = KTopX;
 50     CGFloat controllerY = KTopY + 100;
 51     CGFloat currentX = controllerX - 30;
 52     CGFloat currentY = controllerY - 20;
 53     CGFloat endX = controllerX + 30;
 54     CGFloat endY = currentY;
 55     
 56     CGContextMoveToPoint(ctx, currentX, currentY);
 57     CGContextAddQuadCurveToPoint(ctx, controllerX, controllerY, endX, endY);
 58     
 59     CGContextStrokePath(ctx);
 60 
 61     
 62 }
 63 
 64 - (void)drawEyes : (CGRect)rect CGContextRef : (CGContextRef)ctx
 65 {
 66     // 画镜架
 67     CGFloat leftGlassesX = KTopX - KTopRadius;
 68     CGFloat leftGlassesY = KTopY;
 69     
 70     CGFloat rightGlassesX = KTopX + KTopRadius;
 71     CGFloat rightGlassesY = leftGlassesY;
 72     
 73     CGContextMoveToPoint(ctx, leftGlassesX, leftGlassesY);
 74     CGContextAddLineToPoint(ctx, rightGlassesX, rightGlassesY);
 75     CGContextSetLineWidth(ctx, 15.0);
 76     
 77     CGContextStrokePath(ctx);
 78     
 79     // 2
 80     CGContextRestoreGState(ctx);
 81     
 82     // 画眼睛
 83     
 84     // 大外圆
 85     CGFloat leftBigOutCircleRedius = KTopRadius - 50;
 86     // 圆心
 87     CGFloat leftBigOutCircleX = KTopX - leftBigOutCircleRedius + 1;
 88     CGFloat leftBigOutCircleY = KTopY;
 89     
 90     CGContextAddArc(ctx, leftBigOutCircleX, leftBigOutCircleY, leftBigOutCircleRedius, 0, 2 * M_PI, 0);
 91     
 92     CGFloat rightBigOutCircleRedius = leftBigOutCircleRedius;
 93     // 圆心
 94     CGFloat rightBigOutCircleX = KTopX + leftBigOutCircleRedius - 1;
 95     CGFloat rightBigOutCircleY = leftBigOutCircleY;
 96     
 97     CGContextAddArc(ctx, rightBigOutCircleX, rightBigOutCircleY, rightBigOutCircleRedius, 0, 2 * M_PI, 0);
 98     
 99     [[UIColor darkGrayColor] set];
100     
101     CGContextFillPath(ctx);
102     
103     // 大内圆
104     CGFloat leftBigInsideCircleRedius = leftBigOutCircleRedius - 10;
105     // 圆心
106     CGFloat leftBigInsideCircleX = leftBigOutCircleX;
107     CGFloat leftBigInsideCircleY = leftBigOutCircleY;
108     
109     CGContextAddArc(ctx, leftBigInsideCircleX, leftBigInsideCircleY, leftBigInsideCircleRedius, 0, 2 * M_PI, 0);
110     
111     CGFloat rightBigInsideCircleRedius = leftBigInsideCircleRedius;
112     // 圆心
113     CGFloat rightBigInsideCircleX = rightBigOutCircleX;
114     CGFloat rightBigInsideCircleY = rightBigOutCircleY;
115     
116     CGContextAddArc(ctx, rightBigInsideCircleX, rightBigInsideCircleY, rightBigInsideCircleRedius, 0, 2 * M_PI, 0);
117     
118     [[UIColor whiteColor] set];
119     
120     CGContextFillPath(ctx);
121     
122     // 小外圆
123     CGFloat leftSmallOutCircleRedius = 8;
124     // 圆心
125     CGFloat leftSmallOutCircleX = KTopX - 20;
126     CGFloat leftSmallOutCircleY = KTopY;
127     
128     CGContextAddArc(ctx, leftSmallOutCircleX, leftSmallOutCircleY, leftSmallOutCircleRedius, 0, 2 * M_PI, 0);
129     
130     CGFloat rightSmallOutCircleRedius = leftSmallOutCircleRedius;
131     // 圆心
132     CGFloat rightSmallOutCircleX = KTopX + leftSmallOutCircleRedius + 13;
133     CGFloat rightSmallOutCircleY = leftSmallOutCircleY;
134     
135     CGContextAddArc(ctx, rightSmallOutCircleX, rightSmallOutCircleY, rightSmallOutCircleRedius, 0, 2 * M_PI, 0);
136     
137     [[UIColor colorWithRed:80/255.f green:20/255.f blue:6/255.f alpha:1.0] set];
138     
139     CGContextFillPath(ctx);
140     
141     // 小内圆
142     CGFloat leftSmallInsideCircleRedius = 4;
143     // 圆心
144     CGFloat leftSmallInsideCircleX = leftSmallOutCircleX;
145     CGFloat leftSmallInsideCircleY = leftSmallOutCircleY;
146     
147     CGContextAddArc(ctx, leftSmallInsideCircleX, leftSmallInsideCircleY, leftSmallInsideCircleRedius, 0, 2 * M_PI, 0);
148     
149     CGFloat rightSmallInsideCircleRedius = leftSmallInsideCircleRedius;
150     // 圆心
151     CGFloat rightSmallInsideCircleX = rightSmallOutCircleX;
152     CGFloat rightSmallInsideCircleY = rightSmallOutCircleY;
153     
154     CGContextAddArc(ctx, rightSmallInsideCircleX, rightSmallInsideCircleY, rightSmallInsideCircleRedius, 0, 2 * M_PI, 0);
155     
156     [[UIColor blackColor] set];
157     
158     CGContextFillPath(ctx);
159     
160     // 光点
161     CGFloat leftWhiteCircleRedius = 2;
162     // 圆心
163     CGFloat leftWhiteCircleX = leftSmallOutCircleX - leftWhiteCircleRedius - 1;
164     CGFloat leftWhiteCircleY = leftSmallOutCircleY - leftSmallInsideCircleRedius + 1;
165     
166     CGContextAddArc(ctx, leftWhiteCircleX, leftWhiteCircleY, leftWhiteCircleRedius, 0, 2 * M_PI, 0);
167     
168     CGFloat rightWhiteCircleRedius = leftWhiteCircleRedius;
169     // 圆心
170     CGFloat rightWhiteCircleX = rightSmallOutCircleX - leftWhiteCircleRedius - 1;
171     CGFloat rightWhiteCircleY = leftWhiteCircleY;
172     
173     CGContextAddArc(ctx, rightWhiteCircleX, rightWhiteCircleY, rightWhiteCircleRedius, 0, 2 * M_PI, 0);
174     
175     [[UIColor whiteColor] set];
176     
177     CGContextFillPath(ctx);
178 }
179 
180 - (void)drawBody : (CGRect)rect CGContextRef : (CGContextRef)ctx
181 {
182     CGFloat topX = KTopX;
183     CGFloat topY = KTopY;
184     CGFloat topRadius = KTopRadius;
185     CGContextAddArc(ctx, topX, topY, topRadius, - M_PI, 0, 0);
186     
187     CGFloat rightLineX = topX + topRadius;
188     CGFloat rightLineY = topY + 150;
189     CGContextAddLineToPoint(ctx, rightLineX, rightLineY);
190     
191     CGFloat bottomX = topX;
192     CGFloat bottomY = rightLineY;
193     CGFloat bottomRadius = topRadius;
194     CGContextAddArc(ctx, bottomX, bottomY, bottomRadius, 0, M_PI, 0);
195     
196     CGContextClosePath(ctx);
197     
198     [[UIColor colorWithRed:252/255.f green:218/255.f blue:0.0 alpha:1.0] set];
199     
200     CGContextFillPath(ctx);
201 }
202 - (void)drawHair : (CGRect)rect CGContextRef : (CGContextRef)ctx
203 {
204     CGFloat x = KTopX;
205     CGFloat y = KTopY - KTopRadius - 20;
206     CGContextMoveToPoint(ctx, x, y);
207     
208     CGFloat endX = KTopX;
209     CGFloat endY = y + 25;
210     CGContextAddLineToPoint(ctx, endX, endY);
211     
212     CGContextMoveToPoint(ctx, x + 15, y + 2);
213     CGContextAddLineToPoint(ctx, endX + 10, endY + 1);
214     
215     CGContextMoveToPoint(ctx, x + 30, y + 4);
216     CGContextAddLineToPoint(ctx, endX + 20, endY + 2);
217     
218     CGContextMoveToPoint(ctx, x - 15, y + 2);
219     CGContextAddLineToPoint(ctx, endX - 10, endY + 1);
220     
221     CGContextMoveToPoint(ctx, x - 30, y + 4);
222     CGContextAddLineToPoint(ctx, endX - 20, endY + 2);
223     
224     CGContextMoveToPoint(ctx, x + 45, y + 8);
225     CGContextAddLineToPoint(ctx, endX + 30, endY + 4);
226     
227     CGContextMoveToPoint(ctx, x + 60, y + 16);
228     CGContextAddLineToPoint(ctx, endX + 40, endY + 8);
229     
230     CGContextMoveToPoint(ctx, x - 45, y + 8);
231     CGContextAddLineToPoint(ctx, endX - 30, endY + 4);
232     
233     CGContextMoveToPoint(ctx, x - 60, y + 16);
234     CGContextAddLineToPoint(ctx, endX - 40, endY + 8);
235     
236     CGContextStrokePath(ctx);
237     
238     
239 }
240 @end
View Code