在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
swift版的CircleView
效果图
源码 // // CircleView.swift // CircleView // // Created by YouXianMing on 15/10/7. // Copyright © 2015年 YouXianMing. All rights reserved. // import UIKit class CircleView: UIView { // MARK: - 变量 var lineWidth : CGFloat = 1 var lineColor : UIColor = UIColor.blackColor() var clockWise : Bool = false var startAngle : CGFloat = 0 var duration : NSTimeInterval = 0.2 private var circleLayer : CAShapeLayer! // MARK: - Public Method /** 构建view,让参数生效 */ func buildView() { let size = bounds.size let point = CGPoint(x: size.height / 2, y: size.width / 2) let radius = size.width / 2 - lineWidth / 2 var tmpStartAngle : CGFloat = 0 var tmpEndAngle : CGFloat = 0 if (clockWise == true) { tmpStartAngle = -radian(Double(180 - startAngle)); tmpEndAngle = radian(Double(180 + self.startAngle)); } else { tmpStartAngle = radian(Double(180 - self.startAngle)); tmpEndAngle = -radian(Double(180 + 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 } /** 绘制圆形百分比 - parameter percent: 百分比 - parameter animated: 是否开启动画 */ 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 override init(frame: CGRect) { super.init(frame: frame) createCircleLayer() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: - Private Method private func radian(degrees : Double) -> CGFloat { return CGFloat((M_PI * degrees) / 180) } private func createCircleLayer() { circleLayer = CAShapeLayer() circleLayer.frame = self.bounds self.layer.addSublayer(circleLayer) } } // // ViewController.swift // CircleView // // Created by YouXianMing on 15/10/7. // Copyright © 2015年 YouXianMing. All rights reserved. // import UIKit class ViewController: UIViewController { var eventTimer : NSTimer! var circleView : CircleView! override func viewDidLoad() { super.viewDidLoad() eventTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerEvent", userInfo: nil, repeats: true) circleView = CircleView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) circleView.lineWidth = 1 circleView.lineColor = UIColor.blackColor() circleView.duration = 0.25 circleView.clockWise = true circleView.startAngle = 90 circleView.center = view.center circleView.buildView() view.addSubview(circleView) } func timerEvent() { circleView.changeToPercent(CGFloat(arc4random() % 101) / 100, animated: true) } }
说明 参数查看并没有OC那么直白.
|
请发表评论