ios - 显示 CAShapeLayers 数组

标签 ios swift cashapelayer

我创建了一个 CAShapeLayers 数组,以便在不同图层上以不同颜色绘制圆弧的不同部分,这是我的代码:

import UIKit

class ViewController: UIViewController {

    var level = 0.0
    var old_level = 0.75

    var progressLayer: [CAShapeLayer] = []
    var circle = UIView()

    override func viewDidLoad() {

        circle = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

        view.addSubview(circle)

        update_curve(value: 10)
        update_curve(value: 20)
        update_curve(value: 10)

    }

    func update_curve(value: Double){

        level = value*(1.5/100)+0.75

        progressLayer.append(CAShapeLayer())

        var progressPath = UIBezierPath(arcCenter: CGPoint(x: progressLayer[layerindex].frame.size.width/2, y: progressLayer[layerindex].frame.size.height/2), radius: CGFloat(100), startAngle: CGFloat(M_PI*old_level), endAngle:CGFloat(level*M_PI), clockwise: true)

        circle.layer.addSublayer(progressLayer[layerindex])
        progressLayer[layerindex].frame = view.bounds

        progressLayer[layerindex].path = progressPath.cgPath

        progressLayer[layerindex].fillColor = UIColor.clear.cgColor

        progressLayer[layerindex].strokeColor = generateRandomColor()

        progressLayer[layerindex].lineWidth = 20.0

        let animation2 = CABasicAnimation(keyPath: "strokeEnd")

        animation2.fromValue = 0.0
        animation2.toValue = 1.0
        animation2.duration = 1
        animation2.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

        progressLayer[layerindex].add(animation2, forKey: "drawLineAnimation")

        layerindex += 1
        old_level = level
    }

    func generateRandomColor() -> UIColor {
        let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
        let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
        let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black

        return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
    }
}

但是当我运行这段代码时,iPhone 的屏幕上没有出现任何内容,但我没有收到错误消息。我做错了什么?

最佳答案

我刚刚意识到progressLayer的框架是在定义progressPath之后定义的。 update_curve 函数的代码应为:

func update_curve(value: Double){

    level = value*(1.5/100)+0.75

    progressLayer.append(CAShapeLayer())

    progressLayer[layerindex].frame = view.bounds

    var progressPath = UIBezierPath(arcCenter: CGPoint(x: progressLayer[layerindex].frame.size.width/2, y: progressLayer[layerindex].frame.size.height/2), radius: CGFloat(100), startAngle: CGFloat(M_PI*old_level), endAngle:CGFloat(level*M_PI), clockwise: true)

    circle.layer.addSublayer(progressLayer[layerindex])

    progressLayer[layerindex].path = progressPath.cgPath

    progressLayer[layerindex].fillColor = UIColor.clear.cgColor

    progressLayer[layerindex].strokeColor = generateRandomColor()

    progressLayer[layerindex].lineWidth = 20.0

    let animation2 = CABasicAnimation(keyPath: "strokeEnd")

    animation2.fromValue = 0.0
    animation2.toValue = 1.0
    animation2.duration = 1
    animation2.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)

    progressLayer[layerindex].add(animation2, forKey: "drawLineAnimation")

    layerindex += 1
    old_level = level
}

关于ios - 显示 CAShapeLayers 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037685/

相关文章:

ios - 使用动画更改 CAShapeLayer 的位置

iOS if conditional 似乎没有按预期进行评估。愚蠢的错误或有趣的事情?

ios - 如何将 UIImage 数据复制到剪贴板/UIPasteboard?我该如何测试它?

ios - SwiftUI 在自定义 TextField 上切换事件/非事件线条颜色

ios - 如何使用闭包返回数组

swift - 如何在点击按钮时启动渐变动画?

ios - 是否存在将我的 cordova 应用程序发布到 IOS 的服务?

ios - 根据出生日期计算年龄

ios - 在Swift中以编程方式注销Facebook应用

ios - 如何选择 CAShapeLayer