swift版的CircleView

swift版的CircleView-LMLPHP

效果图

swift版的CircleView-LMLPHP

源码

//
// 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 =
var lineColor : UIColor = UIColor.blackColor()
var clockWise : Bool = false
var startAngle : CGFloat =
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 / , y: size.width / )
let radius = size.width / - lineWidth / var tmpStartAngle : CGFloat =
var tmpEndAngle : CGFloat = if (clockWise == true) { tmpStartAngle = -radian(Double( - startAngle));
tmpEndAngle = radian(Double( + self.startAngle)); } else { tmpStartAngle = radian(Double( - self.startAngle));
tmpEndAngle = -radian(Double( + 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 =
} /**
绘制圆形百分比 - parameter percent: 百分比
- parameter animated: 是否开启动画
*/
func changeToPercent(var percent : CGFloat, animated : Bool) { if (percent <= ) { percent = ; } else if (percent >= ) { percent = ;
} if (animated) { let basicAnimation : CABasicAnimation! = CABasicAnimation()
basicAnimation.keyPath = "strokeEnd"
basicAnimation.duration = (duration <= ? 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) / )
} 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(, target: self, selector: "timerEvent", userInfo: nil, repeats: true) circleView = CircleView(frame: CGRect(x: , y: , width: , height: ))
circleView.lineWidth =
circleView.lineColor = UIColor.blackColor()
circleView.duration = 0.25
circleView.clockWise = true
circleView.startAngle =
circleView.center = view.center
circleView.buildView() view.addSubview(circleView)
} func timerEvent() { circleView.changeToPercent(CGFloat(arc4random() % ) / , animated: true)
}
}

说明

参数查看并没有OC那么直白.

05-02 09:00