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. JS模块化编程(一):CommonJS,AMD/CMD

    前言 模块化是什么? 为什么采用模块化? 场景: 一个html,因不同的业务需求开发,会不断的引入js文件.另外,a.js和b.js中的变量或函数必须是全局的,才能暴露给使用方. <script ...

  2. Android Studio 3.0正式版填坑之路

    原文:https://www.jianshu.com/p/9b25087a5d7d   Android Studio 3.0启动图 序言 总看别人的文章,今天尝试着自己来写一篇.在逛论坛时候,无意间发 ...

  3. cookie之困

    参见http://yun.baidu.com/share/link?shareid=1575530779&uk=1795493794 cookie三元组(name,domain,path),它 ...

  4. Javascript中Object常用方法学习

    1.Object.assign 函数(对象)(JavaScript) 将来自一个或多个源对象中的值复制到一个目标对象.语法: Object.assign(target, ...sources ); 此 ...

  5. eclipse launching workspace太慢的解决方法

    这几天eclipse调试Android项目的时候反应超慢,右下显示launching workspace就不怎么动了,今天终于卡的受不了了,在网上搜了写方法,设置了下总算好点了,现在把方法贴出来,跟大 ...

  6. Wireshark数据抓包教程之Wireshark的基础知识

    Wireshark数据抓包教程之Wireshark的基础知识 Wireshark的基础知识 在这个网络信息时代里,计算机安全始终是一个让人揪心的问题,网络安全则有过之而无不及.Wireshark作为国 ...

  7. BZOJ.4571.[SCOI2016]美味(主席树 贪心)

    题目链接 要求 \(b\ xor\ (a_j+x)\) 最大,应让 \(a_j+x\) 的最高位尽可能与b相反.带个减法Trie树好像很难做?反正我不会. 从最高位开始,如果这位b是0/1,判断是否存 ...

  8. [POJ1144]Network

    来源:Central Europe 1996 思路:Tarjan求割点. 一个点$x$为割点当且仅当: 1.$x$为根结点且有两棵不相交的子树. 2.$x$不为根结点且它的子树中没有可以返回到$x$的 ...

  9. Codeforces Round #281 (Div. 2) B. Vasya and Wrestling 水题

    B. Vasya and Wrestling 题目连接: http://codeforces.com/contest/493/problem/B Description Vasya has becom ...

  10. android studio 使用总结

    网站1:http://stormzhang.com/posts.html 网站2:http://blog.csdn.net/hyr83960944/article/details/38388429