let context = UIGraphicsGetCurrentContext()
// 初始化一个 UIBezierPath 实例
let arcPath = UIBezierPath()
// 构建Arc路径
arcPath.addArcWithCenter(CGPointMake(CGFloat(self.frame.size.width/2),CGFloat(self.frame.size.height/2)),radius: CGFloat(Config.CC_ARC_DRAW_RADIUS),startAngle: CGFloat(DegreesToRadians(startAngle)),endAngle: CGFloat(DegreesToRadians(startAngle + Config.CC_ARC_DRAW_DEGREE)),clockwise: true)
// 把路径添加到当前绘图的上下文
CGContextAddPath(context,arcPath.CGPath)
// 设置线段宽度
CGContextSetLineWidth(context,CGFloat(Config.CC_ARC_DRAW_WIDTH))
// 设置线段颜色
CGContextSetStrokeColorWithColor(context,strokeColor)
// 绘制
CGContextStrokePath(context)
func addArcWithCenter(center: CGPoint,radius: CGFloat,startAngle: CGFloat,endAngle: CGFloat,clockwise: Bool)
static let CC_ACTIVITY_INDICATOR_VIEW_NUMBEROFFRAMES: Int = 8
static let CC_ACTIVITY_INDICATOR_VIEW_WIDTH: CGFloat = 40
static let CC_ARC_DRAW_PADDING: CGFloat = 3.0
static let CC_ARC_DRAW_DEGREE: CGFloat = 39.0
static let CC_ARC_DRAW_WIDTH: CGFloat = 6.0
static let CC_ARC_DRAW_RADIUS: CGFloat = 10.0
static let CC_ARC_DRAW_COLORS = [UIColor(red: 242/255.0,green: 242/255.0,blue: 242/255.0,alpha: 1.0).CGColor,UIColor(red: 230/255.0,green: 230/255.0,blue: 230/255.0,UIColor(red: 179/255.0,green: 179/255.0,blue: 179/255.0,UIColor(red: 128/255.0,green: 128/255.0,blue: 128/255.0,alpha: 1.0).CGColor]
static func CCActivityIndicatorViewFrameImage(frame: Int,_ scale: CGFloat) -> UIImage {
// 创建一个基于位图的上下文(context)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CC_ACTIVITY_INDICATOR_VIEW_WIDTH,CC_ACTIVITY_INDICATOR_VIEW_WIDTH),false,scale)
let context = UIGraphicsGetCurrentContext()
var startDegree = Config.CC_ARC_DRAW_PADDING
for index in 1...8 {
let arcPath = UIBezierPath()
let center = CGPointMake(Config.CC_ACTIVITY_INDICATOR_VIEW_WIDTH / 2,Config.CC_ACTIVITY_INDICATOR_VIEW_WIDTH / 2)
let startAngle = CGFloat(DegreesToRadians(Double(startDegree)))
let endAngle = CGFloat(DegreesToRadians(Double(startDegree + Config.CC_ARC_DRAW_DEGREE)))
arcPath.addArcWithCenter(center,radius: Config.CC_ARC_DRAW_RADIUS,startAngle: startAngle,endAngle: endAngle,clockwise: true)
CGContextAddPath(context,arcPath.CGPath)
startDegree += Config.CC_ARC_DRAW_DEGREE + (Config.CC_ARC_DRAW_PADDING * 2)
CGContextSetLineWidth(context,Config.CC_ARC_DRAW_WIDTH)
let colorIndex = abs(index - frame)
let strokeColor = Config.CC_ARC_DRAW_COLORS[colorIndex]
CGContextSetStrokeColorWithColor(context,strokeColor)
CGContextStrokePath(context)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func startAnimating()
func stopAnimating()
func isAnimating() -> Bool
func startAnimating () {
func animate () {
self.animating = true
let animationDuration: CFTimeInterval = 0.8
// 构造一组动画所需的images
var animationImages = [CGImageRef]()
for frame in 1...Config.CC_ACTIVITY_INDICATOR_VIEW_NUMBEROFFRAMES {
animationImages.append(Config.CCActivityIndicatorViewFrameImage(frame,UIScreen.mainScreen().nativeScale).CGImage)
}
// 使用Core Animation关键帧CAKeyframeAnimation构建动画
let animation = CAKeyframeAnimation(keyPath: "contents")
animation.calculationMode = kCAAnimationDiscrete
animation.duration = animationDuration
animation.repeatCount = HUGE
animation.values = animationImages
animation.removedOnCompletion = false
animation.fillMode = kCAFillModeBoth
self.layer.addAnimation(animation,forKey: "contents")
}
if !self.animating {
animate()
}
}
func stopAnimating () {
self.animating = false
self.layer.removeAnimationForKey("contents")
self.layer.contents = Config.CCActivityIndicatorViewFrameImage(0,UIScreen.mainScreen().nativeScale).CGImage
}