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. Python Extension 编译问题

    Python 的一大好处是调用C扩展程序相当方便,但是 Windows 下的话,编译器版本是一个大问题.以Python27为例,官方版本时采用VC2008编译的.在setup.py中添加Extensi ...

  2. 002 @RequestMapping的说明

    一:修饰方法 1.举例 2.效果 二:修饰类 1.测试类 2.index.jsp 3.效果 ---------------- 三:映射请求请求方法 1.使用语法 分别是method 2.Method的 ...

  3. Node.js包的依赖及版本号(转)

    原文:  http://www.cnphp6.com/archives/64130 Node.js最重要的一个文件就是package.json,其中的配置参数决定了功能.例如下面就是一个例子 { &q ...

  4. 001.WordPress建站部署

    一 WordPress简介 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站.也可以把 WordPress当作一个内容管理系统( ...

  5. 循序渐进学.Net Core Web Api开发系列【7】:项目发布到CentOS7

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇讨论如 ...

  6. 使用Metasploit工作区

    使用Metasploit工作区   Metasploit将所有数据都存储在PostgeSQL服务器中的msf数据库.渗透测试人员经常要使用Metasploit同时执行多个任务.为了避免数据混杂,Met ...

  7. 【精选】Jupyter Notebooks里的TensorFlow图可视化

    [精选]Jupyter Notebooks里的TensorFlow图可视化   https://mp.weixin.qq.com/s?src=11&timestamp=1503060682&a ...

  8. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  9. Optimizing Oracle RAC

    Oracle Real Application Clusters (RAC) databases form an increasing proportion of Oracle database sy ...

  10. IAR EWAR 内联汇编 Error[Og010], Error [Og005], Error [Og006]

    Error [Og005] + [Og006] when using inline assembler EW targets: 430, ARM, AVR EW component: C/C++ co ...