簡體   English   中英

顯示CAShapeLayers的數組

[英]Display an array of CAShapeLayers

我創建了一個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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM