2016-12-31 71 views
0
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") 

    } 

我正在用上面的代碼繪製圓圈。它在屏幕上的按鈕周圍繪製一個圓圈,表示從創建按鈕開始經過的時間。此代碼有效,但是當我進入主屏幕並返回時,無論剩下多少時間,屏幕上的所有圈子都會完全填充。如果在應用程序中,我切換到另一頁然後回來,它修復了它。當視圖重新進入前景時恢復繪製CAShapeLayer?

我從這個火力點查詢

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

內調用makeACircle有一次我在做出一個按鈕足夠的信息,我使按鈕,然後調用makeACircle。

有關如何防止圓圈不出現的任何想法,就好像他們已經達到strokeEnd當我從主頁加載時?

回答

0

當你暫停動畫,

  • 看那presentation layer,並保存其strokeEnd

  • 你需要停止動畫,這樣做。您可能需要從表示層的值中設置strokeEnd以避免發生任何不和諧的更改。

  • 查看自開​​始動畫以來經過了多長時間(例如,比較暫停時的CACurrentMediaTime()與啓動動畫時的值)以便確定動畫中剩餘的時間。

然後,當你重新呈現層,你現在有fromValuestrokeEnd什麼剩餘duration是。

,可以是有用的另一件事是,設置動畫的delegate,然後用animationDidStopnil你的「進行中」的狀態變量,如果動畫成功完成(即,如果finished是真的)。