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. Chapter 3 Phenomenon——2

    I had enough trouble not falling down when the ground was dry; it might be safer for me to go back t ...

  2. Linux-(top,free)

    top命令 1.命令格式: top [参数] 2.命令功能: 显示当前系统正在执行的进程的相关信息,包括进程ID.内存占用率.CPU占用率等. top命令是Linux下常用的性能分析工具,能够实时显示 ...

  3. Docker运行操作系统环境(BusyBox&Alpine&Debian/Ubuntu&CentOS/Fedora)

    目前常用的Linux发行版主要包括Debian/Ubuntu系列和CentOS/Fedora系列.前者以自带软件包版本较新而出名:后者则宣称运行更稳定一些.选择哪个操作系统取决于读者的具体需求.同时, ...

  4. 在word中优雅地插入代码

    PlanetB:带行号 http://www.planetb.ca/syntax-highlight-word   Pygments(推荐):不带行号,多种样式可选 http://pygments.o ...

  5. ibatis(sqlmap)中 #与$的使用区别

    在sqlmap文件中不使用“#VALUE#”来原样(参数对应什么类型,就当什么类型,比如拼凑的内容为string则自动加上了‘’)读取,而是$VALUE$方式来读取,即不加任何的东西,比如单引号啥的, ...

  6. AI小白快上车!这是发往高薪职位的车!

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云AI中心发表于云+社区专栏 AI到底有多火?看看下面这组数据: 腾讯研究院<2017全球人工智能人才白皮书>报告中提 ...

  7. 简单canvas刮刮乐

    好久没玩canvas了,随便写点效果玩玩.要开始重新拾起这门牛x的技术了,工作中不一定能用得上,以后说不定就能发挥用处了. <!DOCTYPE html> <html lang=&q ...

  8. grafana安装

    1 wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.4-1.x86_64.rpm2 sudo ...

  9. H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报

    摘要:初略算了算大概有20天没有写博客了,原本是打算1月1号元旦那天写一个年终总结的,博客园里大佬们都在总结过去,迎接将来,看得我热血沸腾,想想自己也工作快2年了,去年都没有去总结一下,今年势必要总结 ...

  10. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...