Swift-绘制弧度

作者: FlyElephant | 来源:发表于2017-02-12 21:32 被阅读795次

iOS绘制弧度有两个函数,一个设置中心点另外一个是设置点到点,
<pre><code>` public func addArc(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)

public func addArc(tangent1End: CGPoint, tangent2End: CGPoint, radius: CGFloat)`</code></pre>

我们设置起点弧度为0,终点弧度为1.5 * PI(等于270角度),最后的clockwise的设置为true.
<pre><code>` UIGraphicsBeginImageContext(self.view.bounds.size);

    let context:CGContext = UIGraphicsGetCurrentContext()!
    
    let path:CGMutablePath = CGMutablePath()
  
    context.addArc(center:CGPoint(x: 150, y: 150), radius: 50, startAngle: 0, endAngle: 1.5*CGFloat(M_PI), clockwise: true)
    
    context.move(to: CGPoint(x: 150, y: 100))
    context.addLine(to: CGPoint(x: 150, y: 150))
    context.addLine(to: CGPoint(x: 200, y: 150))
    
    context.addPath(path)
    
    UIColor.gray.setFill()
    UIColor.red.setStroke()
    
    context.drawPath(using: .fillStroke)
    
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    
    let imgView:UIImageView = UIImageView.init(image: image)
    self.view.addSubview(imgView)`</code></pre>

效果图:


FlyElephant.png

顺时针设为false:
<pre><code>context.addArc(center:CGPoint(x: 150, y: 150), radius: 50, startAngle: 0, endAngle: 1.5*CGFloat(M_PI), clockwise: false)</code></pre>
效果图:

FlyElephant.png

圆角矩形

通过线和线之间的相切,我们也可以获取圆角,代码如下:
<pre><code>` UIGraphicsBeginImageContext(self.view.bounds.size);

    let context:CGContext = UIGraphicsGetCurrentContext()!
    
    let path:CGMutablePath = CGMutablePath()
    
    context.move(to: CGPoint(x: 100, y: 300))
    
    context.addArc(tangent1End: CGPoint(x: 200, y: 300), tangent2End: CGPoint(x: 200, y: 400), radius: 5.0)
    context.addArc(tangent1End: CGPoint(x: 200, y: 400), tangent2End: CGPoint(x: 100, y: 400), radius: 5.0)
    
    context.addArc(tangent1End: CGPoint(x: 100, y: 400), tangent2End: CGPoint(x: 100, y: 300), radius: 5.0)
    context.addArc(tangent1End: CGPoint(x: 100, y: 300), tangent2End: CGPoint(x: 200, y: 300), radius: 5.0)
    
    context.addPath(path)
    
    UIColor.gray.setFill()
    UIColor.red.setStroke()
    
    context.drawPath(using: .fillStroke)
    
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    
    let imgView:UIImageView = UIImageView.init(image: image)
    self.view.addSubview(imgView)`</code></pre>

效果图如下:


FlyElephant.png

相关文章

  • Swift-绘制弧度

    iOS绘制弧度有两个函数,一个设置中心点另外一个是设置点到点, ` public func addArc(...

  • Canvas入门3

    绘制弧度 基本概念角度:一个圆360度,一个半圆是180度弧度:一个圆2Π,一个半圆Π 角度转换弧度弧度 = 角度...

  • Canvas绘制时钟效果

    Cnavas绘制时钟 背景图的绘制(大圆、数字、小圆点),掌握基础知识:圆的绘制(arc方法),关于圆的弧度的计算...

  • swift-绘制虚线

    参考:http://www.jianshu.com/p/f9009968c735 效果图: 代码: 效果图2: 代码:

  • Swift-虚线绘制

    iOS中有时候会遇到虚线绘制,关于绘制虚线有两种方式,一种是通过Context绘制图片通过UIImageView展...

  • 【总第20期】英国博赞思维导图管理师认证班Rose第四幅《阅读笔

    【主题】人类简史阅读笔记 【绘制目的】:整理书中的知识点 【亮点】:中心图 【绘制后检核的可提升点】:分支的弧度还...

  • canvas杂笔记-圆形

    js中,Math.PI 计算角度弧度,Math.PI = 3.14 = 180° 绘制圆形: 2*Math.PI ...

  • Flutter 的曲线的弧度的绘制

    入口文件 方法文件

  • 动态绘制背景不同弧度的TextView

    先上实现代码: 主要实现代码就一块: 根据角度,确认path路径,添加到绘制页面绘制即可,省却了通过shape 固...

  • swift-类属性

    了解属性之前,需要先了解前面的swift-类结构内容 - swift-类结构源码探寻[https://www.ji...

网友评论

    本文标题:Swift-绘制弧度

    本文链接:https://www.haomeiwen.com/subject/eizaittx.html