封装了一个UILabel并让它显示圆形的边框,UILabel上面显示百份比,而边框则用Animation绘制到整个圆占指定百分比的点。

这只是我个人想的继承一个UILabel实现的,用到两个CAShapeLayer,第一个Layer的作用是画出灰色的背影圆圈,第二个Layer位置放置在第一个Layer的上面,并设置为红色描绘颜色并描绘到插定的位置,之后实现相应的动画效果即可。

import UIKit

class kCircleLabel: UILabel {

    var percent:Double!

    convenience init(percent per:Double,frame:CGRect) {
self.init(frame: frame)
self.percent = per
createCircle()
} override init(frame: CGRect) {
super.init(frame: frame)
} required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} func createCircle() {
self.textAlignment = NSTextAlignment.Center
self.text = "\(percent * 100 )%" //第一个圆形Layer,边框为灰色的
let circleLayer:CAShapeLayer = CAShapeLayer()
circleLayer.lineWidth = 8
//清除填充的颜色
circleLayer.fillColor = UIColor.clearColor().CGColor
//边框的颜色
circleLayer.strokeColor = UIColor.init(red: CGFloat(220.0 / 255.0 ), green: CGFloat(220.0 / 255.0), blue: CGFloat(220.0 / 255.0), alpha: 1.0).CGColor //用贝塞尔曲线画出一个圆
let circlePath:UIBezierPath = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) circleLayer.path = circlePath.CGPath
self.layer.addSublayer(circleLayer) //第二个只描绘到特定位置的弧Layer
let arcLayer:CAShapeLayer = CAShapeLayer() //画出特定的弧
let arcPath:UIBezierPath = UIBezierPath(arcCenter: CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2), radius: self.frame.size.width / 2, startAngle: 0.0, endAngle: CGFloat(360 * percent / 180 * M_PI), clockwise: true) arcLayer.path = arcPath.CGPath
arcLayer.lineWidth = 8
//清除填充的颜色
arcLayer.fillColor = UIColor.clearColor().CGColor
arcLayer.strokeColor = UIColor.redColor().CGColor //弧Layer的动画
let arcAnimation:CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
arcAnimation.fromValue = 0.0
arcAnimation.toValue = 1.0
arcAnimation.duration = 1.5
arcAnimation.removedOnCompletion = false
arcAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) //这是从大到小的动画,适用于整个Layer
let scaleAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = 5.0
scaleAnimation.toValue = 1.0
scaleAnimation.duration = 0.5 arcLayer.addAnimation(arcAnimation, forKey: nil) /*let animationGroup:CAAnimationGroup = CAAnimationGroup()
animationGroup.duration = 1.0
animationGroup.animations = [ arcAnimation, scaleAnimation]*/ self.layer.insertSublayer(arcLayer, above : circleLayer)
self.layer.addAnimation(scaleAnimation, forKey: nil) } // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
//override func drawRect(rect: CGRect) {
// Drawing code
//} }

调用时:

let circleLabel = kCircleLabel(percent: 0.52, frame: CGRect(x:  (self.view.bounds.width - 100.0) / 2, y: 260, width: 100.0, height: 100.0))

self.view.addSubview(circleLabel)

要实现这个效果的关键是要学会怎么使用贝塞尔曲线,并给Layer设置Path。

封装一个UILabel圆形边框显示进度的更多相关文章

  1. 一个UILabel不同部分显示不同颜色

    我们直接来看效果图吧: 需求:就是表格cell里面的状态Label,前面的"状态:"是黑色,后面的状态值是红色,他们在同一个Label上,怎么做呢? 解答:真的是会者不难,难者不会 ...

  2. UISlider显示进度(并且实现图片缩放)

    图片展示效果如下: 其他没什么好说的,直接上代码: RootView.h: #import <UIKit/UIKit.h> @interface RootView : UIView @pr ...

  3. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  4. iOS开发之自己封装一个progressHUD控件

    看了几个轻量级的progress view 我觉得KVNProgress做的最漂亮吧 突然我想为什么我自己不封装一个控件 然后我研究了一下KVNProgress KVN简单的界面是由storyboar ...

  5. ajax上传文件显示进度

    下面要做一个ajax上传文件显示进度的操作,文末有演示地址 这里先上代码: 1.前端代码 upload.html <!DOCTYPE html> <html lang="e ...

  6. 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具

    转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...

  7. [Android] 给图像加入相框、圆形圆角显示图片、图像合成知识

        前一篇文章讲述了Android触屏setOnTouchListener实现突破缩放.移动.绘制和加入水印,继续我的"随手拍"项目完毕给图片加入相框.圆形圆角显示图片和图像合 ...

  8. 手把手从零开始---封装一个vue视频播放器组件

    现在,在网页上播放视频已经越来越流行,但是网上的资料鱼龙混杂,很难找到自己想要的,今天小编就自己的亲身开发体验,手把手从零开始---封装一个vue视频播放器组件. 作为一个老道的前端搬砖师,怎么可能会 ...

  9. 【Winform】使用BackgroundWorker控制进度条显示进度

    许多开发者看见一些软件有进度条显示进度,自己想弄,项目建好后发现并没有自己想象中的那么简单...看了网上很多教程后,写了一个小Demo供网友们参考~~,Demo的网址:http://pan.baidu ...

随机推荐

  1. berkeley db replica机制 - election algorithm

    repmgr_method.c, __repmgr_start_int() 初始2个elect线程. repmgr_elect.c, __repmgr_init_election() __repmgr ...

  2. dialogic d300语音卡驱动重装后启动报错问题解决方法

    dialogic d300 驱动重装后 dlstart  报错解决 问题描述:dlstart  后如下报错 [root@BJAPQ091 data]#dlstop Stopping Dialogic ...

  3. [转]正确使用SQLCipher来加密Android数据库 - 朝野布告

    参考文档:http://www.tuicool.com/articles/eYNFbuA Android本身自带有不加密的数据库SQLite,如果要保存密码之类的敏感数据在本地的话方法一是使用字段加密 ...

  4. iOS自动适配

    自iphone4s以后,苹果先后推出了iphone5.iphone5s.iphone6.iphone6plus.iphone6s.iphone6splus这些新的机型,它们的屏幕大小各有所异,从此给我 ...

  5. 20+ 个很有用的 jQuery 的 Google 地图插件

    转自:http://www.oschina.net/translate/20-useful-jquery-google-maps-plugins Google 地图在寻找我们想要了解的商店或者其它有趣 ...

  6. HTML 中禁用鼠标右键和不能选中文字

    在body中加这句就可以了. <body oncontextmenu="return false" onselectstart="return false" ...

  7. “System.Web.UI.WebControls.Literal”不允许使用子控件

    今天在写下面的代码时遭遇错误——“System.Web.UI.WebControls.Literal”不允许使用子控件('System.Web.UI.WebControls.Literal' does ...

  8. windowsXP用户被禁用导致不能网站登录

    1.查看系统事件,发现弹出如下的错误 2.根据上面的错误,我们很容易就可以判断是禁用了账户引起的 2.1后面进入计算机管理,再进入用户管理 2.2双击点开Internet来宾用于,发现此用户已经停用了 ...

  9. Nginx学习笔记(八) Nginx进程启动分析

    Nginx进程启动分析 worker子进程的执行循环的函数是ngx_worker_process_cycle (src/os/unix/ngx_process_cycle.c). 其中,捕获事件.分发 ...

  10. HTTP权威指南阅读笔记二:URL与资源

    URL中的限制字符: 在URL中,有几个字符被保留起来,有着特殊的含义.有些字符不在定义的US-ASCII可打印字符集中.还有些字符会与某些因特网网关和协议产生混淆,因此不赞成使用. 下面这些字符是U ...