Ich habe eine Animation zum Zeichnen eines Kreises. Ich kombiniere 3 verschiedene Kreisanimationen, um ein stilisiertes Diagramm zu erstellen.Einen Strich automatisch startenEnde Animation
Sie animieren Perfektion, wenn in einer @IBAction von einem Knopf. Aber wie würde ich die Animation automatisch starten, wenn das Storyboard/ViewController geöffnet/navigiert wird?
Animations Code wie folgt:
//NF Colour
let colorNF = UIColor(red: 28/255, green: 117/255, blue: 189/255, alpha: 1)
// Create Holding Rectangle and Start and End Angles
let ovalStartAngleNF = CGFloat(-90.01 * .pi/180)
let ovalEndAngleNF = CGFloat(10.0 * .pi/180)
let ovalRectNF = CGRect(origin: CGPoint(x: 122.5,y: 83.5), size: CGSize(width: 100, height: 100))
// Create Bezier Path
let ovalPathNF = UIBezierPath()
// Apply Attributes to Bezier Path
ovalPathNF.addArc(withCenter: CGPoint(x: ovalRectNF.midX, y: ovalRectNF.midY),
radius: ovalRectNF.width/2,
startAngle: ovalStartAngleNF,
endAngle: ovalEndAngleNF, clockwise: true)
// Styling of the Curve
let progressLineNF = CAShapeLayer()
progressLineNF.path = ovalPathNF.cgPath
progressLineNF.strokeColor = colorNF.cgColor
progressLineNF.fillColor = UIColor.clear.cgColor
progressLineNF.lineWidth = 20.0
progressLineNF.lineCap = kCALineCapRound
// Add Curve to Viewport
self.view.layer.addSublayer(progressLineNF)
// Animated the 'Stroke End' from 0 to 1 over 3 Seconds
let animateStrokeEndNF = CABasicAnimation(keyPath: "strokeEnd")
animateStrokeEndNF.duration = 3.0
animateStrokeEndNF.fromValue = 0.0
animateStrokeEndNF.toValue = 1.0
// Add the Animation to the Progress Line
progressLineNF.add(animateStrokeEndNF, forKey: "animate stroke end animation")
Bewegen Sie den Animationscode in Ihre 'viewDidLoad'. – Joe
@Joe Ich habe es gerade versucht, und die Kreise werden sofort ausgezogen, anstatt von 0 auf 1 zu animieren. Irgendwelche anderen Ideen? –
Versuchen Sie, die Animationsdauer auf einen höheren Wert> 3 (animateStrokeEndNF.duration = 10.0) zu ändern und versuchen Sie, Ihren Code in 'viewDidAppear oder viewWillAppear' zu verschieben, um eine Startup-Verzögerung zu erzeugen? – Joe