Swift-easinganimation drawing a doughnut animation
Effect
Source
Https://github.com/YouXianMing/Swift-Animations
////Circleview.swift//swift-animations////Created by youxianming on 16/8/16.//copyright©2016 year youxianming. All rights reserved.//Import UIKit//mark:public Class:circleviewclassCircleview:uiview {//mark:convenience init.convenience init (frame:cgrect, Linewidth:cgfloat, Linecolor:uicolor, Clockwise:bool, STARTDEGREE:CGFL Oat) {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{ifPlinewidth <=0{return 1}Else{returnPlinewidth}} Set(newval) {plinewidth =newval}} ///Line color, default is black color.var linecolor:uicolor {Get{ifPlinecolor = = Nil {returnUicolor.blackcolor ()}Else{returnPlinecolor}} Set(newval) {Plinecolor =newval}} ///Clock wise or not, the default is true.var Clockwise:bool =true ///Start degrees (0°~ 360°), default is 0.var startdegree:cgfloat =0 //Mark:methods. /** Make the config effective, if you have set all the properties, you must run the "this" method to make the config Effectiv E.*/func makeeffective () {Let size=bounds.size Let Radius= Size.width/2.0-LineWidth/2.0var tmpstartangle:cgfloat var tmpendangle:cgfloatifClockwise = =true{Tmpstartangle=-radianfromdegrees ( the-startdegree) Tmpendangle= Radianfromdegrees ( the+Startdegree)} Else{Tmpstartangle= Radianfromdegrees ( the-startdegree) Tmpendangle=-radianfromdegrees ( the+Startdegree)} Let Circlepath= Uibezierpath (Arccenter:cgpointmake (Size.Height/2, Size.width/2), Radius:radius, Startangle:tmpstartangle, Endangle:tmpendangle, cl ockwise:clockwise) Pcirclelayer.path=Circlepath.cgpath Pcirclelayer.fillcolor=Uicolor.clearcolor (). Cgcolor Pcirclelayer.strokecolor=Linecolor.cgcolor pcirclelayer.linewidth=linewidth pcirclelayer.strokeend=0 } /** 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=valueifStrokestartvalue <=0{Strokestartvalue=0 } Else ifStrokestartvalue >=1{Strokestartvalue=1 } ifAnimated = =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=valueifStrokestartvalue <=0{Strokestartvalue=0 } Else ifStrokestartvalue >=1{Strokestartvalue=1 } ifAnimated = =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. OverrideInit (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") } Privatevar pcirclelayer:cashapelayer!Privatevar plinewidth:cgfloat! =1 Privatevar plinecolor:uicolor! =Uicolor.blackcolor ()PrivateFunc radianfromdegrees (degrees:cgfloat)CGFloat {return(CGFloat (M_PI) * degrees)/180.0 }}
Swift-easinganimation drawing a doughnut animation