Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用进度循环动画更新uilabel百分比_Ios_Swift_Animation_Uilabel - Fatal编程技术网

Ios 使用进度循环动画更新uilabel百分比

Ios 使用进度循环动画更新uilabel百分比,ios,swift,animation,uilabel,Ios,Swift,Animation,Uilabel,我有一个圆圈,它正在为自己制作动画。它被赋予一个介于0.01和1.0之间的数字,然后在一个圆中设置动画 我在它的中间有一个标签叫做“代码> PraseStRels。我希望标签随着圆圈的动画一起计数。因此,如果该值为0.12,则标签应显示12% 以下是“我的圆”动画的代码: func animateView(toValue: Double, strokeColor: UIColor) { let screenWidth = self.view.frame.size.width

我有一个圆圈,它正在为自己制作动画。它被赋予一个介于0.01和1.0之间的数字,然后在一个圆中设置动画

<>我在它的中间有一个标签叫做“代码> PraseStRels<代码>。我希望标签随着圆圈的动画一起计数。因此,如果该值为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
                }
var电流:双=0.0
设i=电流*100
设最大值=0.1*100
如果我
您应该检查表示层的值,以获取动画属性的当前值

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)%"
        }

    }
func printValue(){
让currentLayer=progressCircle.presentation()
current=currentLayer?值(forKeyPath:“strokeEnd”)为?浮点;
打印(“当前\(当前)”)
设i=电流!*100
设max:Float=100
如果我

调用方法printValue,无论您想在何处显示当前值,代码在此部分中断:
current=currentLayer?.valueForKey(“strokeEnd”)as!浮动