IOS开发之——绘制基本形状
<p>文章搬运来源:https://blog.csdn.net/Calvin_zhou/article/details/111941368<br>作者:PGzxc</p>
<p><strong>对iOS开发感兴趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相关方面的知识,群里还有我整理的有关于面试的一些资料,欢迎大家加群,大家一起开车</strong></p>
<h2 id="一-概述">一 概述</h2>
<p>本文介绍基本图形的绘制:</p>
<ul>
<li>三角形</li>
<li>矩形</li>
<li>圆形</li>
<li>椭圆</li>
<li>圆弧</li>
<li>封闭圆弧</li>
</ul>
<h2 id="二-绘制三角形">二 绘制三角形</h2>
<h3 id="21-绘制代码">2.1 绘制代码</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=;
CGPoint startP=CGPointMake(10, 10);
;
;
;
;
//;
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
[setFill];
[setStroke];
CGContextSetLineWidth(ctx, 15);
//4.渲染上下文
//CGContextStrokePath(ctx);
//CGContextFillPath(ctx);
CGContextDrawPath(ctx,kCGPathFillStroke);
}
</code></pre>
<h3 id="22-给三角形添加文字">2.2 给三角形添加文字</h3>
<pre><code>- (UILabel *)label{
if (_label==nil) {
UILabel *label=[initWithFrame:CGRectMake(0, 0, 250, 100)];
label.text=@"s";
label.textColor=;
label.font=;
label.textAlignment=NSTextAlignmentCenter;
;
}
return _label;
}
- (void)awakeFromNib
{
self.label;
}
</code></pre>
<h3 id="23-效果图">2.3 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-4b310d91f31448e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h2 id="三-绘制矩形">三 绘制矩形</h2>
<h3 id="31-代码">3.1 代码</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *paht=;
//3.把路径添加到上下文
CGContextAddPath(ctx, paht.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
</code></pre>
<h3 id="32-效果图">3.2 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-a3a60a3d16734376.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h2 id="四-绘制圆">四 绘制圆</h2>
<h3 id="41-代码1">4.1 代码1</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=;
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
</code></pre>
<h3 id="42-代码2">4.2 代码2</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *paht=;
paht=;
//3.把路径添加到上下文
CGContextAddPath(ctx, paht.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
</code></pre>
<h3 id="43-效果图">4.3 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-8f19854ddfe2fc13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h2 id="五-绘制椭圆">五 绘制椭圆</h2>
<h3 id="51-代码">5.1 代码</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
UIBezierPath *path=;
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
</code></pre>
<h3 id="52-效果图">5.2 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-584f5a1b5a86eff0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h2 id="六-圆弧">六 圆弧</h2>
<h3 id="61-代码">6.1 代码</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
CGPoint center=CGPointMake(125, 125);
CGFloat radius=100;
CGFloat startA=0;
CGFloat endA=M_PI;
UIBezierPath *path=;
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
CGContextStrokePath(ctx);
}
</code></pre>
<h3 id="62-效果图">6.2 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-698c0d4b38f24c43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h2 id="七-封闭圆弧">七 封闭圆弧</h2>
<h3 id="71-代码">7.1 代码</h3>
<pre><code>- (void)drawRect:(CGRect)rect {
//1.获取上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接路径
CGPoint center=CGPointMake(125, 125);
CGFloat radius=100;
CGFloat startA=0;
CGFloat endA=M_PI_2;
UIBezierPath *path=;
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.渲染上下文
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);
}
</code></pre>
<h3 id="72-效果图">7.2 效果图</h3>
<p><img src="https://upload-images.jianshu.io/upload_images/25330009-e424c1332d0ca3bf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p><br><br>
来源:https://www.cnblogs.com/fadaijun/p/14416685.html
頁:
[1]