使用进度圈swift

时间:2016-10-24 13:54:06

标签: ios swift animation uilabel

我有一个动画本身的圆圈。给出一个介于0.01和1.0之间的数字,然后在一个圆圈中进行动画制作。

我在其中间有一个名为progressLabel的标签。我希望标签能够像圆圈一样动画。因此,如果该值为0.12,则标签应显示12%。

以下是我的圈子动画的代码:

func animateView(toValue: Double, strokeColor: UIColor) {

        let screenWidth = self.view.frame.size.width
        let screenHeight = self.view.frame.size.height

        let circle = UIView(frame: CGRectMake((screenWidth / 2) - (150 / 2), (screenHeight / 2) - (150 / 2), 150, 150)) // viewProgress is a UIView
        circle.backgroundColor = UIColor.clearColor()
        view.addSubview(circle)

        var progressCircle = CAShapeLayer()
        var backgroundCircle = CAShapeLayer()
        progressCircle.frame = view.bounds
        backgroundCircle.frame = view.bounds

        let lineWidth:CGFloat = 20
        let rectFofOval = CGRectMake(lineWidth / 2, lineWidth / 2, circle.bounds.width - lineWidth, circle.bounds.height - lineWidth)

        let circlePath = UIBezierPath(ovalInRect: rectFofOval)

        progressCircle = CAShapeLayer ()
        progressCircle.path = circlePath.CGPath
        progressCircle.strokeColor = UIColor.whiteColor().CGColor
        progressCircle.fillColor = UIColor.clearColor().CGColor
        progressCircle.lineWidth = 20.0
        progressCircle.frame = view.bounds
        progressCircle.lineCap = "round"

        backgroundCircle = CAShapeLayer ()
        backgroundCircle.path = circlePath.CGPath
        backgroundCircle.strokeColor = strokeColor.CGColor
        backgroundCircle.fillColor = UIColor.clearColor().CGColor
        backgroundCircle.lineWidth = 20.0
        backgroundCircle.frame = view.bounds
        backgroundCircle.lineCap = "round"

        circle.layer.addSublayer(backgroundCircle)
        circle.layer.addSublayer(progressCircle)
        circle.transform = CGAffineTransformRotate(circle.transform, CGFloat(-M_PI_2))

        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = toValue
        animation.duration = 1
        animation.fillMode = kCAFillModeForwards
        animation.removedOnCompletion = false
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)

        progressCircle.addAnimation(animation, forKey: nil)
    }

我试过了,但标签只是显示0.0%..

var current: Double = 0.0

                let i = current * 100
                let max = 0.1 * 100


                if i < max {


                    self.progressLabel.text = "\(current)%"
                    current += 0.01 * 100
                }

1 个答案:

答案 0 :(得分:1)

您应该检查表示层的值以获取动画属性的当前值。

func printValue() {
        let currentLayer = progressCircle.presentation()
        current = currentLayer?.value(forKeyPath: "strokeEnd") as? Float;
        print("current \(current)")

        let i = current! * 100
        let max:Float = 100


        if i < max {
            self.progressLabel.text = "\(current! * 100)%"
        }

    }

从您想要显示当前值的位置调用方法printValue