当视图重新进入前景时恢复绘制 CAShape图层?


func makeACircle(circle: UIView, stokeStart: Double, duration: Double){

var progressCircle = CAShapeLayer();
let centerPoint = CGPoint (x: circle.bounds.width / 2, y: circle.bounds.width / 2);
let circleRadius : CGFloat = circle.bounds.width / 2
var circlePath = UIBezierPath(arcCenter: centerPoint, radius: circleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true    );
progressCircle = CAShapeLayer ();
progressCircle.path = circlePath.cgPath;
progressCircle.strokeColor = UIColor.white.cgColor;
progressCircle.fillColor = UIColor.clear.cgColor;
progressCircle.lineWidth = 10;
progressCircle.strokeStart = 0;
progressCircle.strokeEnd = 1;
progressCircle.borderWidth = 1
progressCircle.borderColor = UIColor.black.cgColor
circle.layer.addSublayer(progressCircle);
// progressCircle.clipsToBounds = false
self.view.addSubview(circle)
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Set the animation duration appropriately
animation.duration = duration
// Animate from 0 (no circle) to 1 (full circle)
animation.fromValue = stokeStart
animation.toValue = 1
// 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.
progressCircle.strokeEnd = 1.0
// Do the actual animation
progressCircle.add(animation, forKey: "animateCircle")
}

我正在用上面的代码画圆圈。它在屏幕上的按钮周围画一个圆圈,表示自创建按钮以来经过的时间。这段代码有效,但是当我转到主屏幕并返回时,无论剩余多少时间,屏幕上的所有圆圈都被完全填满。如果在应用程序中我切换到另一个页面然后回来,它会修复它。

我从这个火力库查询中调用makeACircle

currentUserRef?.child("invitedToPosts").queryOrdered(byChild: "timestamp").queryStarting(atValue: cutoff).observe(.childAdded) { (snapshot: FIRDataSnapshot) in 

一旦我有足够的关于要制作的按钮的信息,我就会制作按钮,然后调用makeACircle。

关于如何防止圆圈在我从主页加载时没有出现好像它们已经到达笔画结束的任何想法?

暂停动画时,

  • 查看表示层并保存其strokeEnd

  • 您需要停止动画,这样做。您可能希望根据表示层的值设置strokeEnd以避免任何不和谐的更改。

  • 查看自启动动画以来经过了多少时间(例如,比较暂停时CACurrentMediaTime()与启动动画时的值),以确定动画中剩余的时间。

然后,当您重新呈现图层时,您现在拥有strokeEndfromValue以及剩余duration的内容。

另一件有用的事情是设置动画的delegate,然后在动画成功完成时使用animationDidStopnil"进行中"状态变量(即,如果finished为 true)。

最新更新