2016-10-24 13 views
1

私は自分自身をアニメーション化しているサークルを持っています。それは0.01と1.0の間の数が与えられ、それはそれの後に円でアニメーション化されます。進捗状況サークルスウィフトのアニメーションでuilabelのパーセンテージを更新

私はその真ん中にprogressLabelというラベルがあります。私は円が動くようにラベルを数えることを望みます。したがって、値が0.12の場合、ラベルには12%が表示されます。私はこれを試してみましたが、ラベルはちょうど0.0%を表示し続ける

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

..あなたは、プレゼンテーション層の値をチェックする必要があります

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

:ここ

は私のサークルのアニメーションのコードですアニメーションプロパティの現在の値を取得します。

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

    } 

コールあなたは、この部分に現在の値に

+0

コードブレークを表示したい場所を、これまでの方法printValue: '現在= currentLayer .valueForKey(「strokeEnd」)のように?! Float' –

+0

あなたはどんなエラーを出していますか? –