Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
在Swift 3中转到背景后,ViewDid中的动画似乎不起作用_Swift_Animation_Background_Swift3_Viewdidappear - Fatal编程技术网

在Swift 3中转到背景后,ViewDid中的动画似乎不起作用

在Swift 3中转到背景后,ViewDid中的动画似乎不起作用,swift,animation,background,swift3,viewdidappear,Swift,Animation,Background,Swift3,Viewdidappear,我目前正在Swift 3中开发一个倒计时应用程序。在计数器后面,我创建了一个圆形,它根据计数器设置动画 我的问题很简单:当我单击home并将应用程序放在后台时,当我返回应用程序时,动画不再工作。我不知道为什么。有趣的事实:“绘图”是一个普通的圆,只有边框是动画的。圆的背景仍然正常,只是边界动画不再工作 这是我的密码: override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated)

我目前正在Swift 3中开发一个倒计时应用程序。在计数器后面,我创建了一个圆形,它根据计数器设置动画

我的问题很简单:当我单击home并将应用程序放在后台时,当我返回应用程序时,动画不再工作。我不知道为什么。有趣的事实:“绘图”是一个普通的圆,只有边框是动画的。圆的背景仍然正常,只是边界动画不再工作

这是我的密码:

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

}

您只需在动画代码中再添加一行,即:

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

仅供参考,每次视图出现时,您都会添加一个新的圆形视图。相反,您应该重新使用第一次显示视图时创建的同一实例。很高兴为您提供帮助:)很好的建议!我的问题是:通知我的层设置动画的本地通知,它位于选项卡栏视图控制器的另一个子视图控制器中。如果未将
isRemovedOnCompletion
设置为
false
,则当我转到该视图控制器时,动画不会发生,甚至调用了添加动画代码。现在开始工作了!谢谢