public void SetAnimationOffset(nfloat animOffset, Action <UIColor> block) { if (colorAnimLayer.AnimationForKey(SliderFillColorAnim) != null) { colorAnimLayer.TimeOffset = animOffset; pathLayer.FillColor = ((CAShapeLayer)colorAnimLayer.PresentationLayer).FillColor; block?.Invoke(OpaqueColor); } }
public void UpdateLayers() { UIFont textFont = UIFont.SystemFontOfSize(_textLayer.FontSize); nfloat minimumPlane = (float)Math.Min(Frame.Width, Frame.Height); _textLayer.FontSize = Bounds.Height / 2.75f * 0.75f; _textLayer.SetFont(textFont.Name); _textLayer.String = $"{_progressValue * 100}%"; var offset = (Bounds.Height - textFont.LineHeight) / 2; _textLayer.Frame = new CGRect(0, offset, Bounds.Width, Bounds.Height); _textLayer.Hidden = IsIndeterminate || !IsActive; if (IsActive) { _shadowLayer.RemoveAnimation(ANIMATION_SHRINK); _arcLayer.RemoveAnimation(ANIMATION_SHRINK); _shadowLayer.RemoveAnimation(ANIMATION_OPACITYDOWN); _arcLayer.RemoveAnimation(ANIMATION_OPACITYDOWN); if (null == _shadowLayer.AnimationForKey(ANIMATION_GROW)) { // grow arc to full size _pathGrowAnimation.SetFrom(_shadowLayer.Path); _pathGrowAnimation.SetTo(CreateArcPath(minimumPlane)); _shadowLayer.AddAnimation(_pathGrowAnimation, ANIMATION_GROW); _arcLayer.AddAnimation(_pathGrowAnimation, ANIMATION_GROW); _arcLayer.AddAnimation(_pathOpacityUpAnimation, ANIMATION_OPACITYUP); _shadowLayer.AddAnimation(_pathOpacityUpAnimation, ANIMATION_OPACITYDOWN); _pathOpacityDownAnimation.From = NSNumber.FromFloat(_arcLayer.Opacity); _pathOpacityDownAnimation.To = NSNumber.FromFloat(1); } if (IsIndeterminate) { _textLayer.Hidden = true; if (null == _arcLayer.AnimationForKey(ANIMATION_ROTATION)) { SetupRotationAnimation(); _rotationAnimation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseIn); _arcLayer.AddAnimation(_rotationAnimation, ANIMATION_ROTATION); } if (null == _arcLayer.AnimationForKey(ANIMATION_STROKE)) { SetupStrokeAnimation(); _arcLayer.AddAnimation(_strokeAnimation, ANIMATION_STROKE); } } else { // Don't force a change in the stroke if it's currently being animated. // It will get set to the correct value when the animation completes anyway. if (null == _arcLayer.AnimationForKey(ANIMATION_STROKE)) { _arcLayer.StrokeEnd = ProgressValue; } if (null == _shadowLayer.AnimationForKey(ANIMATION_COLOR)) { _shadowLayer.AddAnimation(_colorAnimation, ANIMATION_COLOR); } } } else { _shadowLayer.RemoveAnimation(ANIMATION_GROW); _arcLayer.RemoveAnimation(ANIMATION_GROW); _shadowLayer.RemoveAnimation(ANIMATION_OPACITYUP); _arcLayer.RemoveAnimation(ANIMATION_OPACITYUP); if (null == _shadowLayer.AnimationForKey(ANIMATION_SHRINK)) { // shrink arc away _pathShrinkAnimation.SetFrom(_shadowLayer.Path); _pathShrinkAnimation.SetTo(CreateArcPath(5)); _pathOpacityDownAnimation.From = NSNumber.FromFloat(_arcLayer.Opacity); _pathOpacityDownAnimation.To = NSNumber.FromFloat(0); _shadowLayer.AddAnimation(_pathShrinkAnimation, ANIMATION_SHRINK); _arcLayer.AddAnimation(_pathShrinkAnimation, ANIMATION_SHRINK); _arcLayer.AddAnimation(_pathOpacityDownAnimation, ANIMATION_OPACITYDOWN); _shadowLayer.AddAnimation(_pathOpacityDownAnimation, ANIMATION_OPACITYDOWN); } } }
private void SetupStrokeAnimation() { if (null == _strokeAnimation) { _strokeAnimation = CABasicAnimation.FromKeyPath("strokeEnd"); _strokeAnimation.AnimationStopped += (x, y) => { if (_strokeAnimation.RemovedOnCompletion) { // Disable animation because the CALayer will attempt to animate // the stroke from the PREVIOUS StrokeEnd value (not the currently animated value). CATransaction.DisableActions = true; _arcLayer.StrokeEnd = this.ProgressValue; CATransaction.DisableActions = false; } else { // Reinitialize animation parameters SetupStrokeAnimation(); if (IsIndeterminate) { _arcLayer.AddAnimation(_strokeAnimation, ANIMATION_STROKE); } else if (null != _arcLayer.AnimationForKey(ANIMATION_STROKE)) { // If the animation is currently running, let's phase it out. // Ending at the current 'Progress Value'. _strokeAnimation.To = NSNumber.FromNFloat(this.ProgressValue); _strokeAnimation.Duration = _rotationAnimation.Duration * 1.5f; // ...then have it removed so it no longer recurs. _strokeAnimation.RemovedOnCompletion = true; // Queue it up for one final animation... _arcLayer.AddAnimation(_strokeAnimation, ANIMATION_STROKE); } } }; } var presentationLayer = _arcLayer.PresentationLayer as CAShapeLayer ?? _arcLayer; if (presentationLayer.StrokeEnd > 0.75f) { _strokeAnimation.From = NSNumber.FromNFloat(presentationLayer.StrokeEnd);//NSNumber.FromNFloat(0.1f); _strokeAnimation.To = NSNumber.FromNFloat(0.1f); } else if (presentationLayer.StrokeEnd > 0.1f) { _strokeAnimation.From = NSNumber.FromNFloat(presentationLayer.StrokeEnd);//NSNumber.FromNFloat(0.75f); _strokeAnimation.To = NSNumber.FromNFloat(0.1f); } else { _strokeAnimation.From = NSNumber.FromNFloat(presentationLayer.StrokeEnd);//NSNumber.FromNFloat(0.1f); _strokeAnimation.To = NSNumber.FromNFloat(0.75f); } _strokeAnimation.Duration = _rotationAnimation.Duration * 1.5f; _strokeAnimation.Cumulative = true; _strokeAnimation.FillMode = CAFillMode.Forwards; _strokeAnimation.TimingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.EaseInEaseOut); _strokeAnimation.RemovedOnCompletion = false; }