第一步:

先创建一个OneView类,并在storyboard里边拖拽一个UIview,将这个UIview的类改成OneView。如图所示:

第二步:

在新创建的Oneview里,补齐下列代码:

 //
// OneView.m
// 03-drawImage
//
// Created by jerry on 15/7/20.
// Copyright (c) 2015年 jerry. All rights reserved.
// #import "OneView.h" @implementation OneView -(void)drawRect:(CGRect)rect
{
// [self drawImage];
[self drawYellowPop:rect];
}
//
-(void)drawYellowPop:(CGRect)rect
{
// 先获取图片上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置头及颜色
[self setHeaderWithCGContextRef:ctx andRect:rect]; [self setMouthWith:ctx andRect:rect]; [self setGlassesWith:ctx andRect:rect]; [self setHairWith:ctx andRect:rect]; }
/**
* 画嘴
*
* @param ctx <#ctx description#>
* @param rect <#rect description#>
*/
-(void)setMouthWith:(CGContextRef)ctx andRect:(CGRect)rect
{
// 设置中间点
CGFloat CenterX = rect.size.width * 0.5;
CGFloat CenterY = rect.size.width * 0.5; // 设置当前点
CGFloat marginX = ;
CGFloat marginY = ;
CGFloat currenX = CenterX - marginX;
CGFloat currenY = CenterY - marginY;
CGContextMoveToPoint(ctx, currenX, currenY); // 结束点
CGFloat endX = CenterX + marginX;
CGFloat endY = currenY;
// 贝塞尔曲线
CGContextAddQuadCurveToPoint(ctx, CenterX, CenterY, endX, endY); // 设置颜色
[[UIColor blackColor]set];
// 渲染
CGContextStrokePath(ctx);
}
// 设置眼镜
-(void)setGlassesWith:(CGContextRef)ctx andRect:(CGRect)rect
{
CGFloat LineX = rect.size.width * 0.5 - ; // 减半径
CGFloat LineY = ;
// 画一条黑线
CGContextMoveToPoint(ctx,LineX , LineY);
// 结束点
CGFloat EndLineX = LineX + * ;
CGFloat EndLineY = LineY;
CGContextAddLineToPoint(ctx,EndLineX, EndLineY);
CGContextSetLineWidth(ctx, );
[[UIColor blackColor]set];
CGContextStrokePath(ctx); // 2.画眼
// 注释:画眼就是画圆
CGFloat leftEyesY = LineY;
CGFloat leftEyesRadius = ;
CGFloat leftEyesX = rect.size.width *0.5 - leftEyesRadius;
CGFloat leftEyesStartAngle = ;
CGFloat leftEyesEndAngle = M_PI * ;
[[UIColor redColor]set];
CGContextAddArc(ctx, leftEyesX, leftEyesY, leftEyesRadius, leftEyesStartAngle, leftEyesEndAngle, );
CGContextFillPath(ctx); CGFloat rightEyesX = leftEyesX + leftEyesRadius * ;
CGFloat rightEyesY = leftEyesY;
CGFloat rightEyesSstartAngle = ;
CGFloat rightEyesRadius = leftEyesRadius;
CGFloat rightEyesEndAngle = leftEyesEndAngle;
CGContextAddArc(ctx, rightEyesX, rightEyesY, rightEyesRadius, rightEyesSstartAngle, rightEyesEndAngle, );
[[UIColor redColor]set];
CGContextFillPath(ctx); // 镜片
CGFloat leftGlassesRadius = ;
CGFloat leftGlassesX = leftEyesX;
CGFloat leftGlassesY = LineY;
CGContextAddArc(ctx, leftGlassesX, leftGlassesY, leftGlassesRadius, , M_PI * , ); CGFloat rightGlassesRadius = leftGlassesRadius;
CGFloat rightGlassesX = rightEyesX;
CGFloat rightGlassesY = LineY;
CGContextAddArc(ctx, rightGlassesX, rightGlassesY, rightGlassesRadius,, M_PI * , );
[[UIColor whiteColor]set];
CGContextFillPath(ctx); // 眼睛珠
CGFloat leftZhuRadius = ;
CGFloat leftZhuY = LineY;
CGFloat leftZhuX = leftGlassesX +;
CGContextAddArc(ctx, leftZhuX, leftZhuY, leftZhuRadius, , M_PI * , ); CGFloat rightZhuRadius = leftZhuRadius;
CGFloat rightZhuY = LineY;
CGFloat rightZhuX = rightGlassesX - ;
CGContextAddArc(ctx, rightZhuX, rightZhuY, rightZhuRadius, , M_PI * , ); [[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:]set];
CGContextFillPath(ctx); // 左瞳孔
CGFloat leftTongRadius = ;
CGFloat leftTongY = leftZhuY;
CGFloat leftTontX = leftZhuX;
CGContextAddArc(ctx, leftTontX, leftTongY, leftTongRadius, , M_PI * , ); // 右瞳孔
CGFloat rightTongRadius = leftTongRadius;
CGFloat rightTongX = rightZhuX;
CGFloat rightTongY = rightZhuY;
CGContextAddArc(ctx, rightTongX, rightTongY, rightTongRadius, , M_PI *, );
[[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:]set];
CGContextFillPath(ctx); // 左聚光
CGFloat leftJuRadius = ;
CGFloat leftJuX = leftTontX - ;
CGFloat leftJuY =leftTongY - ;
CGContextAddArc(ctx, leftJuX, leftJuY, leftJuRadius, , M_PI * , ); // 右聚光
CGFloat rightJuRadius = leftJuRadius;
CGFloat rightJuX = rightTongX - ;
CGFloat rightJuY = rightTongY - ;
CGContextAddArc(ctx, rightJuX, rightJuY, rightJuRadius, , M_PI *, );
[[UIColor whiteColor]set];
CGContextFillPath(ctx);
} -(void)setHeaderWithCGContextRef:(CGContextRef)ctx andRect:(CGRect)rect
{
// 画图片
CGFloat topX = rect.size.width * 0.5; // 确定x 轴的点
CGFloat topY = ;
CGFloat topRadius = ; // 半径
CGFloat topStartAngle = ; // 起点
CGFloat topEndAngle = M_PI;
// 0 是顺时针 1 是逆时针
CGContextAddArc(ctx, topX, topY, topRadius, topStartAngle, topEndAngle, );
// 以上一个终点为起点画直线
CGFloat leftLineX = topX - topRadius;
CGFloat leftLineY = topY + ;
CGContextAddLineToPoint(ctx, leftLineX, leftLineY); CGFloat downX = topX;
CGFloat downY = leftLineY;
CGContextAddArc(ctx, downX, downY, topRadius,topEndAngle , topStartAngle, );
// 关闭路径
CGContextClosePath(ctx);
// 填充背景颜色
[[UIColor yellowColor]set];
//显示在view
CGContextFillPath(ctx);
}
/**
* 设置头发
*
*/
- (void)setHairWith:(CGContextRef)ctx andRect:(CGRect)rect
{
// 第一根头发
CGFloat hairStartX = rect.size.width *0.5;
CGFloat hairStartY = ;
CGContextMoveToPoint(ctx, hairStartX, hairStartY);
CGFloat hairEndX = rect.size.width * 0.5 + ;
CGFloat hairEndY = ;
CGContextAddLineToPoint(ctx, hairEndX, hairEndY); // 第二根头发
CGFloat hairTwoStartX = rect.size.width * 0.5 + ;
CGFloat hairTwoStartY = ;
CGContextMoveToPoint(ctx, hairTwoStartX, hairTwoStartY);
CGFloat hairTowEndX = rect.size.width * 0.5 + ;
CGFloat hairTowEndY = ;
CGContextAddLineToPoint(ctx, hairTowEndX, hairTowEndY); // 第三根头发
CGFloat hairThreeStartX = rect.size.width * 0.5 - ;
CGFloat hairThreeStartY = ;
CGContextMoveToPoint(ctx, hairThreeStartX, hairThreeStartY);
CGFloat hairThreeEndX = rect.size.width * 0.5 - ;
CGFloat hairThreeEndY = ;
CGContextAddLineToPoint(ctx, hairThreeEndX, hairThreeEndY); // 第四个头发
CGFloat hairFourStartX = rect.size.width * 0.5 - ;
CGFloat hairFourStartY = ;
CGContextMoveToPoint(ctx, hairFourStartX, hairFourStartY);
CGFloat hairFourEndX = rect.size.width *0.5 - ;
CGFloat hairFourEndY = ;
CGContextAddLineToPoint(ctx, hairFourEndX, hairFourEndY); // 第五根头发
CGFloat hairFiveStartX = rect.size.width * 0.5 + ;
CGFloat hairFiveStartY = ;
CGContextMoveToPoint(ctx, hairFiveStartX, hairFiveStartY);
CGFloat hairFiveEndX = rect.size.width * 0.5 + ;
CGFloat hairFiveEndY = ;
CGContextAddLineToPoint(ctx, hairFiveEndX, hairFiveEndY); [[UIColor blackColor]set];
CGContextSetLineWidth(ctx, );
CGContextStrokePath(ctx); }
/**
* 画图片
*/
-(void)drawImage
{
// 用oc代码 可以不用获取图形上下文对象
UIImage *img = [UIImage imageNamed:@""];
[img drawAsPatternInRect:CGRectMake(, , , )]; }
@end

最后运行显示的结果如下:

用Quartz 2D画小黄人的更多相关文章

  1. 纯CSS3画出小黄人并实现动画效果

    前言 前两天我刚发布了一篇CSS3实现小黄人动画的博客,但是实现的CSS3动画是基于我在站酷网找到的一张小黄人的jpg格式图片,并自己用PS抠出需要实现动画的部分,最后才完成的动画效果.但是,其实我的 ...

  2. css3实现小黄人

    效果就像这样: 不废话,直接上代码! hrml代码: <!DOCTYPE html> <html> <head lang="zh"> <m ...

  3. [置顶] 几行代码实现ofo首页小黄人眼睛加速感应转动

    最新版的ofo 小黄车的首页小黄人眼睛随重力而转动,感觉有点炫酷,学习一下吧,以下代码是在xamarin android下实现 ofo首页效果图: xamarin android实现效果: 实现思路: ...

  4. Python turtle模块小黄人程序

    讲解Python初级课程的turtle模块,简单粗暴的编写了小黄人的程序.程序还需要进一步优化.难点就是要搞清楚turtle在绘制图形过程中的方向变化. import turtle t = turtl ...

  5. 小黄人IP营销的四种玩法思维导图

    小黄人IP营销的四种玩法思维导图 ------------------------------ 本人微信公众帐号: 心禅道(xinchandao) 本人微信公众帐号:双色球预测合买(ssqyuce)

  6. CSS3实现小黄人动画

    转载请注明出处,谢谢! 每次看到CSS3动画就心痒痒想试一下,记得一个多月前看了白树哥哥的一篇博客,突然开窍,于是拿他提供的demo试了一下,感觉很棒!下图为demo提供的动画帧设计稿. 自己也想说搞 ...

  7. 静态分析第三发 so文件分析(小黄人快跑)

    本文作者:i春秋作家——HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/downl ...

  8. CSS3小黄人

    CSS3实现小黄人 效果图: 代码如下,复制即可使用: <!DOCTYPE HTML> <HTML> <head> <title>CSS3实现小黄人&l ...

  9. 音频算法之小黄人变声 附完整C代码

    前面提及到<大话音频变声原理 附简单示例代码>与<声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码> 都稍微讲过变声的原理和具体实现. 大家都知道,算法 ...

随机推荐

  1. AISing Programming Contest 2019 翻车记

    A:签到. #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> ...

  2. Spring JDBC 数据访问

    Spring JDBC是Spring所提供的持久层技术,它的主要目标是降低使用JDBC API的门槛,以一种更直接,更简介,更简单的方式使用JDBC API, 在Spring JDBC里,仅需做那些与 ...

  3. A Plug for UNIX POJ - 1087(模板题 没啥好说的。。就用了一个map)

    题意: 几种插头,每一种都只有一个,但有无限个插头转换器,转换器(a,b) 意味着 可以把b转换为a,有几个设备,每个设备对应一种插头,求所不能匹配插头的设备数量 这个题可以用二分图做 , 我用的是最 ...

  4. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C

    A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...

  5. BZOJ 3745: [Coci2015]Norma(分治)

    题意 给定一个正整数序列 \(a_1, a_2, \cdots, a_n\) ,求 \[ \sum_{i=1}^{n} \sum_{j=i}^{n} (j - i + 1) \min(a_i,a_{i ...

  6. Android 判断是否有声音在播放

    在Android中,我们可以通过AudioManager来判断是否有声音在播放. 实例1: 源码地址: PhoneWindowManager.java (frameworks\base\policy\ ...

  7. hdu 2158 最短区间版大家来找碴(尺取法)

    Problem Description 给定一个序列,有N个整数,数值范围为[0,N).有M个询问,每次询问给定Q个整数,可能出现重复值.要求找出一个最短区间,该区间要包含这Q个整数数值.你能找的出来 ...

  8. MATLAB:图像的与、或、非、异或逻辑运算(&、|、~、xor)

    图像的与.或.非.异或逻辑运算涉及到了&.|.~和xor符号 close all;%关闭当前所有图形窗口,清空工作空间变量,清除工作空间所有变量 clc; clear all; I=imrea ...

  9. 在 CentOS 6.x 上安装最新版本的 git

    在 CentOS 的默认仓库中有git,所以最简单的方法是: $ sudo yum install git 这种方法虽然简单,但是一般仓库里的版本更新不及时,比如 CentOS 仓库中的 git 最新 ...

  10. Jenkins-Pipeline 流水线发布部署项目

    node { sh 'mkdir -p cms' dir('cms') { git branch: 'prerelease', credentialsId: '5fb79ef0-4301-4b7c-a ...