使用 Swift 构建自定义的ActivityIndicator View

移动开发 作者: 2024-08-24 16:40:01
本文由@Chun发表于Chun Tips :http://chun.tips/blog/2014/12/11/shi-yong-swift-gou-jian-zi-ding-yi-de-activityindicator-view/ 目前在自己的个人项目里,已经开始使用Swift去编写代码。这篇文章把项目中自己设计的一个ActivityIndicator View展示给大家。 在开始之前,我们先看
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
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67917.html