Swift - EasingAnimation绘制圆环动画

效果

源码

https://github.com/YouXianMing/Swift-Animations

//
// CircleView.swift
// Swift-Animations
//
// Created by YouXianMing on 16/8/16.
// Copyright © 2016年 YouXianMing. All rights reserved.
// import UIKit // MARK: Public class : CircleView class CircleView: UIView { // MARK: Convenience init. convenience init(frame: CGRect, lineWidth : CGFloat, lineColor : UIColor, clockWise : Bool, startDegree : CGFloat) { self.init(frame : frame)
self.lineWidth = lineWidth
self.lineColor = lineColor
self.clockWise = clockWise
self.startDegree = startDegree
self.makeEffective()
} // MARK: Properties. /// Line width, default is 1.0.
var lineWidth : CGFloat { get { if pLineWidth <= { return } else {return pLineWidth}}
set(newVal) { pLineWidth = newVal}
} /// Line color, default is black color.
var lineColor : UIColor { get { if pLineColor == nil { return UIColor.blackColor()} else {return pLineColor}}
set(newVal) { pLineColor = newVal}
} /// Clock wise or not, default is true.
var clockWise : Bool = true /// Start degrees (0° ~ 360°), default is 0.
var startDegree : CGFloat = // MARK: Methods. /**
Make the config effective, when you set all the properties, you must run this method to make the config effective.
*/
func makeEffective() { let size = bounds.size
let radius = size.width / 2.0 - lineWidth / 2.0 var tmpStartAngle : CGFloat
var tmpEndAngle : CGFloat if clockWise == true { tmpStartAngle = -radianFromDegrees( - startDegree)
tmpEndAngle = radianFromDegrees( + startDegree) } else { tmpStartAngle = radianFromDegrees( - startDegree)
tmpEndAngle = -radianFromDegrees( + startDegree)
} let circlePath = UIBezierPath(arcCenter: CGPointMake(size.height / , size.width / ),
radius: radius, startAngle: tmpStartAngle, endAngle: tmpEndAngle, clockwise: clockWise)
pCircleLayer.path = circlePath.CGPath
pCircleLayer.fillColor = UIColor.clearColor().CGColor
pCircleLayer.strokeColor = lineColor.CGColor
pCircleLayer.lineWidth = lineWidth
pCircleLayer.strokeEnd =
} /**
Stroke start animation. - parameter value: StrokeStart value, range is [0, 1].
- parameter easingFunction: Easing function enum value.
- parameter animated: Animated or not.
- parameter duration: The animation's duration.
*/
func strokeStart(value : Double, easingFunction : EasingFunction, animated : Bool, duration : NSTimeInterval) { var strokeStartValue = value if strokeStartValue <= { strokeStartValue = } else if strokeStartValue >= { strokeStartValue =
} if animated == true { let easingValue = EasingValue(withFunction: easingFunction, frameCount: Int(duration * 60.0))
let keyAnimation = CAKeyframeAnimation(keyPath: "strokeStart")
keyAnimation.duration = duration
keyAnimation.values = easingValue.frameValueWith(fromValue: Double(pCircleLayer.strokeStart), toValue: strokeStartValue) pCircleLayer.strokeStart = CGFloat(strokeStartValue)
pCircleLayer.addAnimation(keyAnimation, forKey: nil) } else { CATransaction.setDisableActions(true)
pCircleLayer.strokeStart = CGFloat(strokeStartValue)
CATransaction.setDisableActions(false)
}
} /**
Stroke end animation. - parameter value: StrokeEnd value, range is [0, 1].
- parameter easingFunction: Easing function enum value.
- parameter animated: Animated or not.
- parameter duration: The animation's duration.
*/
func strokeEnd(value : Double, easingFunction : EasingFunction, animated : Bool, duration : NSTimeInterval) { var strokeStartValue = value if strokeStartValue <= { strokeStartValue = } else if strokeStartValue >= { strokeStartValue =
} if animated == true { let easingValue = EasingValue(withFunction: easingFunction, frameCount: Int(duration * 60.0))
let keyAnimation = CAKeyframeAnimation(keyPath: "strokeEnd")
keyAnimation.duration = duration
keyAnimation.values = easingValue.frameValueWith(fromValue: Double(pCircleLayer.strokeEnd), toValue: strokeStartValue) pCircleLayer.strokeEnd = CGFloat(strokeStartValue)
pCircleLayer.addAnimation(keyAnimation, forKey: nil) } else { CATransaction.setDisableActions(true)
pCircleLayer.strokeEnd = CGFloat(strokeStartValue)
CATransaction.setDisableActions(false)
}
} // MARK: Private value & func & system method. override init(frame: CGRect) { super.init(frame: frame)
pCircleLayer = CAShapeLayer()
pCircleLayer.frame = bounds
layer.addSublayer(pCircleLayer)
} required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented")
} private var pCircleLayer : CAShapeLayer!
private var pLineWidth : CGFloat! =
private var pLineColor : UIColor! = UIColor.blackColor() private func radianFromDegrees(degrees : CGFloat) -> CGFloat { return (CGFloat(M_PI) * degrees) / 180.0
}
}

Swift - EasingAnimation绘制圆环动画的更多相关文章

  1. 第165天:canvas绘制圆环旋转动画

    canvas绘制圆环旋转动画——面向对象版 1.HTML 注意引入Konva.js库 <!DOCTYPE html> <html lang="en"> &l ...

  2. 自己定义View之绘制圆环

    一.RingView 自己定义的view,构造器必须重写,至于重写哪个方法,參考例如以下: ①假设须要改变View绘制的图像,那么须要重写OnDraw方法.(这也是最经常使用的重写方式.) ②假设须要 ...

  3. Swift - UITableView展开缩放动画

    Swift - UITableView展开缩放动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // HeaderViewTapA ...

  4. Easing圆环动画

    Easing圆环动画 效果 源码 https://github.com/YouXianMing/Animations // // CircleView.h // YXMWeather // // Cr ...

  5. canvas+js绘制序列帧动画+面向对象

    效果: 素材: 源码:(一般的绘制方式) <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  6. Android 绘制圆环

    使用画圆弧的方式绘制圆环和进度条,使用sweepGradient进行渐变. 参考链接 http://blog.csdn.net/u011494050/article/details/39251239 ...

  7. OpenGL学习进程(10)第七课:四边形绘制与动画基础

        本节是OpenGL学习的第七个课时,下面以四边形为例介绍绘制OpenGL动画的相关知识:     (1)绘制几种不同的四边形: 1)四边形(GL_QUADS) OpenGL的GL_QUADS图 ...

  8. 通通玩blend美工(8)——动态绘制路径动画,画出个萌妹子~

    原文:通通玩blend美工(8)--动态绘制路径动画,画出个萌妹子~ 2年前我在玩Flex的时候就一直有一个疑问,就是如何来实现一个蚊香慢慢烧完的Loading动画呢? 刚经历了某甲方高强度一个月的洗 ...

  9. CSS3绘制弹球动画效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

随机推荐

  1. sql语句添加查询字段

    SELECT * FROM( SELECT ROW_NUMBER() OVER (ORDER BY r.UpdateTime desc) tempRowNum,h.BizID,h.OrgID FROM ...

  2. GetNumber的实现(Python & Java & Golang)

    TCO2014的编程赢取门票的题目,大致是从一个数组(大小为K),可以选取1-K个数,必须保证这n个数是从1-n,返回所有的选取方法个数. 思路:首先是得到从1开始连续的数,保存每个数的个数.然后通过 ...

  3. Using NHibernate with SQLite

    The most convenient method to add NHibernate and SQLite for C# project is using NuGet. You can check ...

  4. JS开发打气球游戏

    JS开发打气球游戏 观视频<月薪4万的程序员有多强?半小时原生JS开发打气球游戏,征服现场数万人!> 清晨,日常打开B站,被首页此视频的标题所吸引,虽一看就是标题党,但还是没能抑制住好奇心 ...

  5. ubuntu16耳机没声音解决

    装完ubuntu16后又装了英伟达的显卡驱动,安装了网易云音乐后,突然发现电脑没声音,使用了如下方法解决 首先用在终端输入如下命令,下载pulseaudio音量控制软件 sudo apt instal ...

  6. ref:linux查看用户登录时间以及命令历史

    ref:https://blog.csdn.net/csdn924618338/article/details/73555725/ 1.查看当前登录用户信息 who命令: who缺省输出包括用户名.终 ...

  7. 004.KVM日常管理1

    一 常用命令 1.1 查看虚机列表及状态 [root@kvm-host ~]# virsh list --all 1.2 连接虚机 [root@kvm-host ~]# virsh console v ...

  8. 浅谈Spring的AOP实现-代理机制

    说起Spring的AOP(Aspect-Oriented Programming)面向切面编程大家都很熟悉(Spring不是这次博文的重点),但是我先提出几个问题,看看同学们是否了解,如果了解的话可以 ...

  9. android 启动 service 的两种方式,及什么时候用哪个 android 什么时候用bindService

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android  什么时候用bindService ============ 启动方式有 ...

  10. 洛谷.3381.[模板]最小费用最大流(zkw)

    题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...