2017-02-06 11 views
2

私は現在、スウィフト3でカウントダウンアプリを開発中です。 カウンタの後ろに、カウンタに従ってアニメーションされた円が作成されました。 私の問題は簡単です。私は家に居るときにアプリをバックグラウンドに置くと、アプリに戻ってももうアニメーションが動かなくなります。 私は理由を知らない。興味深い事実: "描画"は無地の円であり、境界線のみがアニメーション化されています。サークルの背景はまだOKです。それはもはや動作していないボーダーアニメーションだけです。ここでビューのアニメーションが背景に移動した後に動作しない(swift 3)

私のコードです:

override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     let screen = self.view.frame.size.width 
     let y = CGFloat(190) 
     let circleWidth = CGFloat(200) 
     let circleHeight = circleWidth 

     // Create a new CircleView 
     let circleView = CircleView(frame: CGRect(x: (screen/2) - (circleWidth/2), y: y, width: circleWidth, height: circleHeight)) 

     view.addSubview(circleView) 

     // Animate the drawing of the circle over the course of 1 second 
     circleView.animateCircle(TimeInterval(seconds)) 
    } 

CircleViewクラス:

import Foundation 
import UIKit 

class CircleView: UIView{ 
    var circleLayer: CAShapeLayer! 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.backgroundColor = UIColor.clear 

     // Use UIBezierPath as an easy way to create the CGPath for the layer. 
     // The path should be the entire circle. 
     let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2.0, y: frame.size.height/2.0), radius: (frame.size.width - 10)/2, startAngle: CGFloat(-M_PI/2), endAngle: CGFloat(M_PI*1.5), clockwise: true) 

     // Setup the CAShapeLayer with the path, colors, and line width 
     circleLayer = CAShapeLayer() 
     circleLayer.path = circlePath.cgPath 
     circleLayer.fillColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.2).cgColor 
     circleLayer.strokeColor = UIColor(red: 165/255, green: 219/255, blue: 255/255, alpha: 0.8).cgColor 
     circleLayer.lineWidth = 2.0; 

     // Don't draw the circle initially 
     circleLayer.strokeEnd = 0.0 

     // Add the circleLayer to the view's layer's sublayers 
     layer.addSublayer(circleLayer) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 


    func animateCircle(_ duration: TimeInterval) { 
     // We want to animate the strokeEnd property of the circleLayer 
     let animation = CABasicAnimation(keyPath: "strokeEnd") 

     // Set the animation duration appropriately 
     animation.duration = duration 

     // Animate from 0 (no circle) to 1 (full circle) 
     animation.fromValue = 1 
     animation.toValue = 0 

     // Do a linear animation (i.e. the speed of the animation stays the same) 
     animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 

     // Set the circleLayer's strokeEnd property to 1.0 now so that it's the 
     // right value when the animation ends. 
     circleLayer.strokeEnd = 0.0 

     // Do the actual animation 
     circleLayer.add(animation, forKey: "animateCircle") 
    } 

} 

は、あなたが答え:)

+0

となります。ビューが表示されるたびに新しいサークルビューが追加されます。代わりに、初めてビューが表示されたときに作成された同じインスタンスを再使用する必要があります。 – PeejWeej

答えて

1

を持っている場合あなたがする必要があるのは、1以上を追加で事前にありがとうアニメーションコードの行:

animation.isRemovedOnCompletion = false 

となり、コードは

func animateCircle(_ duration: TimeInterval) { 
    // We want to animate the strokeEnd property of the circleLayer 
    let animation = CABasicAnimation(keyPath: "strokeEnd") 

    // Set the animation duration appropriately 
    animation.duration = duration 

    // Animate from 0 (no circle) to 1 (full circle) 
    animation.fromValue = 1 
    animation.toValue = 0 

    // Do a linear animation (i.e. the speed of the animation stays the same) 
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 

    animation.isRemovedOnCompletion = false 

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the 
    // right value when the animation ends. 
    circleLayer.strokeEnd = 0.0 

    // Do the actual animation 
    circleLayer.add(animation, forKey: "animateCircle") 
} 
+0

それは完全に動作します!どうもありがとうございました ! – KevinB

+0

あなたを助けてくれることを幸せにしてください:) –

+0

ニースのアドバイス!私の問題は、タブバービューコントローラの別のサブビューコントローラにある、アニメーション化するためにレイヤに通知するローカル通知です。 'isRemovedOnCompletion'が' false'に設定されていないと、アニメーションコードが呼び出されたとしても、そのビューコントローラに行くとアニメーションは起こりません。今働いている!ありがとう –

関連する問題