Swift version of Circleview
Source
////Circleview.swift//Circleview////Created by youxianming on 15/10/7.//copyright©2015 year youxianming. All rights reserved.//Import UIKitclassCircleview:uiview {//MARK:-Variablevar linewidth:cgfloat=1var linecolor:uicolor=Uicolor.blackcolor () var clockwise:bool=falsevar startangle:cgfloat=0var duration:nstimeinterval=0.2 Privatevar circlelayer:cashapelayer!//MARK:-Public Method /** Build view for parameters to take effect*/func Buildview () {Let size=bounds.size Let point= Cgpoint (X:size.height/2, Y:size.width/2) Let Radius= Size.width/2-LineWidth/2var tmpstartangle:cgfloat=0var tmpendangle:cgfloat=0 if(Clockwise = =true) {Tmpstartangle=-radian (Double ( the-startangle)); Tmpendangle= Radian (Double ( the+self.startangle)); } Else{Tmpstartangle= Radian (Double ( the-self.startangle)); Tmpendangle=-radian (Double ( the+self.startangle)); } Let Circlepath=Uibezierpath (Arccenter:point, Radius:radius, Startangle:tmpstartangle, Endangle:tmpendangle, clockwise: clockwise) Circlelayer.path=Circlepath.cgpath Circlelayer.strokecolor=Linecolor.cgcolor Circlelayer.fillcolor=Uicolor.clearcolor (). Cgcolor Circlelayer.linewidth=linewidth circlelayer.strokeend=0 } /** Draw Circle Percentage-parameter percent: percent-parameter animated: Whether animation is turned on*/func changetopercent (var percent:cgfloat, Animated:bool) {if(Percent <=0) {percent=0; } Else if(Percent >=1) {percent=1; } if(animated) {Let basicanimation:cabasicanimation! =cabasicanimation () Basicanimation.keypath="Strokeend"basicanimation.duration= (Duration <=0?0.2: Duration) Basicanimation.fromvalue=circlelayer.strokeend Basicanimation.tovalue=percent Circlelayer.strokeend=percent Circlelayer.addanimation (basicanimation, Forkey:nil)}Else{catransaction.setdisableactions (true) Circlelayer.strokeend=percent Catransaction.setdisableactions (false) } } //MARK:-System Method OverrideInit (frame:cgrect) {super.init (frame:frame) Createcirclelayer ()} Required init?(coder Adecoder:nscoder) {fatalerror ("Init (coder:) has not been implemented") } //MARK:-Private Method PrivateFunc Radian (degrees:double)CGFloat {returnCGFloat ((M_PI * degrees)/ the) } Privatefunc Createcirclelayer () {Circlelayer=Cashapelayer () circlelayer.frame=self.bounds Self.layer.addSublayer (Circlelayer)}}
////Viewcontroller.swift//Circleview////Created by youxianming on 15/10/7.//copyright©2015 year youxianming. All rights reserved.//Import UIKitclassViewcontroller:uiviewcontroller {var eventtimer:nstimer!var circleview:circleview!Overridefunc viewdidload () {super.viewdidload () Eventtimer= Nstimer.scheduledtimerwithtimeinterval (1, Target:self, selector:"timerevent", Userinfo:nil, repeats:true) Circleview= Circleview (Frame:cgrect (x:0Y:0, Width: -, Height: -)) Circleview.linewidth=1Circleview.linecolor=Uicolor.blackcolor () circleview.duration=0.25circleview.clockwise=trueCircleview.startangle= -Circleview.center=view.center Circleview.buildview () View.addsubview (Circleview)} func timerevent () { Circleview.changetopercent (CGFloat (Arc4random ()%101) / -, Animated:true) }}
Description
Parameter viewing is not as straightforward as OC.
Swift version of Circleview