swift版的CircleView
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的更多相关文章
- Swift版iOS游戏框架Sprite Kit基础教程下册
Swift版iOS游戏框架Sprite Kit基础教程下册 试读下载地址:http://pan.baidu.com/s/1qWBdV0C 介绍:本教程是国内唯一的Swift版的Spritekit教程. ...
- Swift版音乐播放器(简化版),swift音乐播放器
这几天闲着也是闲着,学习一下Swift的,于是到开源社区Download了个OC版的音乐播放器,练练手,在这里发扬开源精神, 希望对大家有帮助! 这个DEMO里,使用到了 AudioPlayer(对音 ...
- 快速排序OC、Swift版源码
前言: 你要问我学学算法在工作当中有什么用,说实话,当达不到那个地步的时候,可能我们不能直接的感觉到它的用处!你就抱着这样一个心态,当一些APP中涉及到算法的时候我不想给其他人画界面!公司的项目也是暂 ...
- iOS可视化动态绘制八种排序过程(Swift版)
前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...
- swift版的GCD封装
swift版的GCD封装 说明 本人针对swift封装了GCD,包括GCDQueue,GCDGroup,GCDTimer以及GCDSemaphore,使用较为便利. 源码 https://github ...
- swift版的StringAttribute
swift版的StringAttribute 效果 源码 https://github.com/YouXianMing/Swift-StringAttribute // // StringAttrib ...
- swift版的元组
swift版的元组 说明 元组的内容并不多,使用的话跟普通变量类似,以下是测试源码: // // ViewController.swift // Tuples // // Created by You ...
- swift版的枚举变量
swift版的枚举变量 swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码 // // ViewController.swift // SwiftEnum ...
- 关东升的iOS实战系列图书 《iOS实战:入门与提高卷(Swift版)》已经上市
承蒙广大读者的厚爱我的 <iOS实战:入门与提高卷(Swift版)>京东上市了,欢迎广大读者提出宝贵意见.http://item.jd.com/11766718.html ...
随机推荐
- Django之模型系统
Django模型简介 Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中 Django 支持 sqlite3, MySQL, oracle,PostgreSQL等数 ...
- Java并发编程笔记之LongAdder和LongAccumulator源码探究
一.LongAdder原理 LongAdder类是JDK1.8新增的一个原子性操作类.AtomicLong通过CAS算法提供了非阻塞的原子性操作,相比受用阻塞算法的同步器来说性能已经很好了,但是JDK ...
- 笔记六:python字符串运算与函数
一:学习内容 字符串运算 字符串函数-strip() 字符串函数-大小写互换 字符串函数-字符串对齐 字符串函数-搜索 字符串函数-替换 字符串函数-split切割 字符串函数-连接join 字符串函 ...
- CentOS rar
基本unrar用法: unrar x -o- -y heidian.rar /var/www/ (把heidian.rar 文件,解压到/var/www/ 目录.要注意/ 结束)
- Web--CSS控制页面(link与import方式区别)
先了解: [1] “Table”和“DIV”这两个网页元素诞生的目的不同,首先Table诞生的目的是为了存储数据,而DIV诞生的目的就是为了架设页面结构 W3C,是World Wide ...
- mybatis使用拦截器显示sql,使用druid配置连接信息
1.显示出sql内容: 新建2个类:MybatisInterceptor :拦截sql,并获得输出sql内容 package com.cpp.core.filter; import java.text ...
- rsyslog 配置 二
转自:https://www.cnblogs.com/cherishry/p/6775163.html rsyslog 配置 二 # rsyslog configuration file # For ...
- C# byte 和 char 转化
C# byte 和 char 可以认为是等价的.但是在文本显示的时候有差异. c# 使用的是unicode字符集,应该和为ascii相互转换 只能转换到字符的unicode编码,或者由unico ...
- Node.js学习笔记(八) --- Node.js的路由模块封装
1 .模块化的方式封装 整理中… 2 .封装仿照 express 的路由整理中…
- java自学-基本数据类型
java中也有对数据的运算处理,java中数据分为常量和变量,常量就是直接固定不变的数据,变量是数据可能发生改变的数据,如下: int a=0; a=1+1; 上边代码,a就是变量,初始为0,接下来 ...