Swift version of Circleview

Source: Internet
Author: User
Tags uikit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.