swift版的CircleView

效果图

源码

//
// CircleView.swift
// CircleView
//
// Created by YouXianMing on 15/10/7.
// Copyright © 2015年 YouXianMing. All rights reserved.
// import UIKit class CircleView: UIView { // MARK: - 变量 var lineWidth : CGFloat =
var lineColor : UIColor = UIColor.blackColor()
var clockWise : Bool = false
var startAngle : CGFloat =
var duration : NSTimeInterval = 0.2 private var circleLayer : CAShapeLayer! // MARK: - Public Method /**
构建view,让参数生效
*/
func buildView() { let size = bounds.size
let point = CGPoint(x: size.height / , y: size.width / )
let radius = size.width / - lineWidth / var tmpStartAngle : CGFloat =
var tmpEndAngle : CGFloat = if (clockWise == true) { tmpStartAngle = -radian(Double( - startAngle));
tmpEndAngle = radian(Double( + self.startAngle)); } else { tmpStartAngle = radian(Double( - self.startAngle));
tmpEndAngle = -radian(Double( + self.startAngle));
} let circlePath = UIBezierPath(arcCenter: point, radius: radius, startAngle: tmpStartAngle, endAngle: tmpEndAngle, clockwise: clockWise) circleLayer.path = circlePath.CGPath
circleLayer.strokeColor = lineColor.CGColor
circleLayer.fillColor = UIColor.clearColor().CGColor
circleLayer.lineWidth = lineWidth
circleLayer.strokeEnd =
} /**
绘制圆形百分比 - parameter percent: 百分比
- parameter animated: 是否开启动画
*/
func changeToPercent(var percent : CGFloat, animated : Bool) { if (percent <= ) { percent = ; } else if (percent >= ) { percent = ;
} if (animated) { let basicAnimation : CABasicAnimation! = CABasicAnimation()
basicAnimation.keyPath = "strokeEnd"
basicAnimation.duration = (duration <= ? 0.2 : duration)
basicAnimation.fromValue = circleLayer.strokeEnd
basicAnimation.toValue = percent
circleLayer.strokeEnd = percent
circleLayer.addAnimation(basicAnimation, forKey: nil) } else { CATransaction.setDisableActions(true)
circleLayer.strokeEnd = percent
CATransaction.setDisableActions(false)
}
} // MARK: - System Method override init(frame: CGRect) { super.init(frame: frame)
createCircleLayer()
} required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented")
} // MARK: - Private Method private func radian(degrees : Double) -> CGFloat { return CGFloat((M_PI * degrees) / )
} private func createCircleLayer() { circleLayer = CAShapeLayer()
circleLayer.frame = self.bounds
self.layer.addSublayer(circleLayer)
}
}
//
// ViewController.swift
// CircleView
//
// Created by YouXianMing on 15/10/7.
// Copyright © 2015年 YouXianMing. All rights reserved.
// import UIKit class ViewController: UIViewController { var eventTimer : NSTimer!
var circleView : CircleView! override func viewDidLoad() {
super.viewDidLoad() eventTimer = NSTimer.scheduledTimerWithTimeInterval(, target: self, selector: "timerEvent", userInfo: nil, repeats: true) circleView = CircleView(frame: CGRect(x: , y: , width: , height: ))
circleView.lineWidth =
circleView.lineColor = UIColor.blackColor()
circleView.duration = 0.25
circleView.clockWise = true
circleView.startAngle =
circleView.center = view.center
circleView.buildView() view.addSubview(circleView)
} func timerEvent() { circleView.changeToPercent(CGFloat(arc4random() % ) / , animated: true)
}
}

说明

参数查看并没有OC那么直白.

swift版的CircleView的更多相关文章

  1. Swift版iOS游戏框架Sprite Kit基础教程下册

    Swift版iOS游戏框架Sprite Kit基础教程下册 试读下载地址:http://pan.baidu.com/s/1qWBdV0C 介绍:本教程是国内唯一的Swift版的Spritekit教程. ...

  2. Swift版音乐播放器(简化版),swift音乐播放器

    这几天闲着也是闲着,学习一下Swift的,于是到开源社区Download了个OC版的音乐播放器,练练手,在这里发扬开源精神, 希望对大家有帮助! 这个DEMO里,使用到了 AudioPlayer(对音 ...

  3. 快速排序OC、Swift版源码

    前言: 你要问我学学算法在工作当中有什么用,说实话,当达不到那个地步的时候,可能我们不能直接的感觉到它的用处!你就抱着这样一个心态,当一些APP中涉及到算法的时候我不想给其他人画界面!公司的项目也是暂 ...

  4. iOS可视化动态绘制八种排序过程(Swift版)

    前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...

  5. swift版的GCD封装

    swift版的GCD封装 说明 本人针对swift封装了GCD,包括GCDQueue,GCDGroup,GCDTimer以及GCDSemaphore,使用较为便利. 源码 https://github ...

  6. swift版的StringAttribute

    swift版的StringAttribute 效果 源码 https://github.com/YouXianMing/Swift-StringAttribute // // StringAttrib ...

  7. swift版的元组

    swift版的元组 说明 元组的内容并不多,使用的话跟普通变量类似,以下是测试源码: // // ViewController.swift // Tuples // // Created by You ...

  8. swift版的枚举变量

    swift版的枚举变量 swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码 // // ViewController.swift // SwiftEnum ...

  9. 关东升的iOS实战系列图书 《iOS实战:入门与提高卷(Swift版)》已经上市

             承蒙广大读者的厚爱我的 <iOS实战:入门与提高卷(Swift版)>京东上市了,欢迎广大读者提出宝贵意见.http://item.jd.com/11766718.html ...

随机推荐

  1. javascript快速入门1--JavaScript前世今生,HelloWorld与开发环境

    JavaScript历史 大概在1992年,一家称作Nombas的公司开始开发一种叫做C--(C-minus-minus,简称Cmm)的嵌入式脚本语言. Cmm背后的理念很简单:一个足够强大可以替代宏 ...

  2. java中的复制数组arraycopy()

    System.arraycopy();//静态方法,在System类中定义,注意copy首字母是小写的 例子: int[] a = {1,2,3,4,5}; int[] b = {9,8,7,6}; ...

  3. Express开发性能优化

    1.使用浏览器缓存 在app.js里添加 var CACHETIME = 60 * 1000 * 60 * 24 * 30; app.use(express.static(path.join(__di ...

  4. C# 代码占用的空间

    是不是代码会占用空间,如果一个程序初始化需要 100M 的代码,那么在他初始化之后,这些代码就没有作用了,他会不会占空间?本文经过测试发现,代码也是会占空间. 我写了2k个垃圾类代码,然后把他放在一个 ...

  5. debian上安装docker ce

    在Debian9上安装Docker CE 使用从包中安装的方式 Docker是一个开源的容器引擎,它有助于更快地交付产品.Docker可将应用程序和基础设施层隔离,并且将基础设施当作程序一样进行管理. ...

  6. [PHP] 重回基础(IO流)

    IO流用来处理设备之间的数据传输 java对数据的操作是通过流的方式 java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流 流按流向分为:输入流,输出流 字符流 抽象基类:Rea ...

  7. java8 lambda 表达式

    lambada 表达式实质上是一个匿名方法,但该方法并非独立执行,而是用于实现由函数式接口定义的唯一抽象方法 使用 lambda 表达式时,会创建实现了函数式接口的一个匿名类实例 可以将 lambda ...

  8. hdu 1430

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  9. K:有限状态自动机

      有限状态自动机是一种特殊的状态机.它表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.有限状态自动机分为两种,一种是 确定有限状态自动机(DFA) ,一种是 非确定有限状态自动机(NF ...

  10. [js高手之路]Node.js模板引擎教程-jade速学与实战1-基本用法

    环境准备: 全局安装jade: npm install jade -g 初始化项目package.json: npm init --yes 安装完成之后,可以使用 jade --help 查看jade ...