private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { var items = AssociatedObject.GetChildren <UIElement>().Where(l => l.RenderTransform is TransformGroup); items = items.Where(l => (l.RenderTransform as TransformGroup).Children.Count == 4); var controls = items?.ToList(); if (controls == null || controls.Count == 0) { return; } Storyboard storyboard = new Storyboard(); for (int i = 0; i < controls.Count; i++) { foreach (var item in Timelines.OfType <DoubleAnimation>()) { TimeSpan span = TimeSpan.FromMilliseconds(i * (Split + item.Duration.TimeSpan.TotalMilliseconds)); TimeSpan end = item.Duration.TimeSpan + span; DoubleAnimationUsingKeyFrames frames = new DoubleAnimationUsingKeyFrames(); //EasingDoubleKeyFrame key1 = new EasingDoubleKeyFrame(item.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); //EasingDoubleKeyFrame key2 = new EasingDoubleKeyFrame(item.From.Value, KeyTime.FromTimeSpan(span)); //EasingDoubleKeyFrame key3 = new EasingDoubleKeyFrame(item.To.Value, KeyTime.FromTimeSpan(end)); //frames.KeyFrames.Add(key1); //frames.KeyFrames.Add(key2); //frames.KeyFrames.Add(key3); EasingDoubleKeyFrame key1 = new EasingDoubleKeyFrame(item.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); frames.KeyFrames.Add(key1); Storyboard.SetTarget(frames, controls[i]); Storyboard.SetTargetProperty(frames, Storyboard.GetTargetProperty(item)); storyboard.Children.Add(frames); DoubleAnimation animation = item.Clone(); animation.BeginTime = span; Storyboard.SetTarget(animation, controls[i]); storyboard.Children.Add(animation); } } storyboard.FillBehavior = FillBehavior.HoldEnd; storyboard.Begin(); }
protected override void RefreshAnimation() { var items = this.GetChildren <UIElement>().Where(l => l.RenderTransform is TransformGroup); items = items.Where(l => (l.RenderTransform as TransformGroup).Children.Count == 4); var controls = items?.ToList(); if (controls == null || controls.Count == 0) { return; } Storyboard storyboard = new Storyboard(); for (int i = 0; i < controls.Count; i++) { foreach (var item in Timelines.OfType <DoubleAnimation>()) { TimeSpan span = TimeSpan.FromMilliseconds(i * (SplitMilliSecond + item.Duration.TimeSpan.TotalMilliseconds)); TimeSpan end = item.Duration.TimeSpan + span; if (item.BeginTime == TimeSpan.Zero) { DoubleAnimationUsingKeyFrames frames = new DoubleAnimationUsingKeyFrames(); EasingDoubleKeyFrame key1 = new EasingDoubleKeyFrame(item.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); frames.KeyFrames.Add(key1); Storyboard.SetTarget(frames, controls[i]); Storyboard.SetTargetProperty(frames, Storyboard.GetTargetProperty(item)); storyboard.Children.Add(frames); } DoubleAnimation animation = item.Clone(); animation.BeginTime = span + animation.BeginTime; Storyboard.SetTarget(animation, controls[i]); storyboard.Children.Add(animation); } foreach (var item in Timelines.OfType <ThicknessAnimation>()) { TimeSpan span = TimeSpan.FromMilliseconds(i * (SplitMilliSecond + item.Duration.TimeSpan.TotalMilliseconds)); TimeSpan end = item.Duration.TimeSpan + span; ThicknessAnimationUsingKeyFrames frames = new ThicknessAnimationUsingKeyFrames(); EasingThicknessKeyFrame key1 = new EasingThicknessKeyFrame(item.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); frames.KeyFrames.Add(key1); Storyboard.SetTarget(frames, controls[i]); Storyboard.SetTargetProperty(frames, Storyboard.GetTargetProperty(item)); storyboard.Children.Add(frames); ThicknessAnimation animation = item.Clone(); animation.BeginTime = span; Storyboard.SetTarget(animation, controls[i]); storyboard.Children.Add(animation); } } storyboard.FillBehavior = FillBehavior.HoldEnd; storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.Begin(); }
/// <summary> 执行方法 </summary> protected override void Invoke(object parameter) { if (this.AssociatedObject == null) { return; } if (this.AutoFindParent) { this.Target = this.AssociatedObject.GetParent <Panel>(); } if (this.Target == null) { this.Target = this.AssociatedObject; } IEnumerable <UIElement> children = null; if (IsUseAll) { children = this.Target.GetChildren <UIElement>().Where(l => l.RenderTransform is TransformGroup); children = children.Where(l => (l.RenderTransform as TransformGroup).Children.Count == 4); children = children.Where(l => (l as FrameworkElement)?.Tag?.ToString() != "Except" && !InvokeRandomSplitAnimationAction.GetIsExcept(l)); } else { if (this.Target is Panel panel) { children = panel.Children?.Cast <UIElement>()?.Where(l => l.RenderTransform is TransformGroup); children = children.Where(l => (l.RenderTransform as TransformGroup).Children.Count == 4 && !InvokeRandomSplitAnimationAction.GetIsExcept(l)); } } if (children == null) { return; } var controls = children?.ToList(); if (controls == null || controls.Count == 0) { return; } if (this.UseIndex) { controls = this.RandomList(controls); } Storyboard storyboard = new Storyboard(); for (int i = 0; i < controls.Count; i++) { if (this.UseOrigin) { controls[i].RenderTransformOrigin = this.GetRandomOrigin(); } foreach (var item in Timelines.OfType <RandomDoubleAnimation>()) { TimeSpan span = TimeSpan.FromMilliseconds(i * (SplitMilliSecond + item.Duration.TimeSpan.TotalMilliseconds)); TimeSpan end = item.Duration.TimeSpan + span; DoubleAnimation animation = item.ConvertTo(); if (item.BeginTime == TimeSpan.Zero) { DoubleAnimationUsingKeyFrames frames = new DoubleAnimationUsingKeyFrames(); EasingDoubleKeyFrame key1 = new EasingDoubleKeyFrame(animation.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); frames.KeyFrames.Add(key1); Storyboard.SetTarget(frames, controls[i]); Storyboard.SetTargetProperty(frames, Storyboard.GetTargetProperty(item)); storyboard.Children.Add(frames); } animation.BeginTime = span + animation.BeginTime; Storyboard.SetTarget(animation, controls[i]); storyboard.Children.Add(animation); } foreach (var item in Timelines.OfType <ThicknessAnimation>()) { TimeSpan span = TimeSpan.FromMilliseconds(i * (SplitMilliSecond + item.Duration.TimeSpan.TotalMilliseconds)); TimeSpan end = item.Duration.TimeSpan + span; ThicknessAnimationUsingKeyFrames frames = new ThicknessAnimationUsingKeyFrames(); EasingThicknessKeyFrame key1 = new EasingThicknessKeyFrame(item.From.Value, KeyTime.FromTimeSpan(TimeSpan.Zero)); frames.KeyFrames.Add(key1); Storyboard.SetTarget(frames, controls[i]); Storyboard.SetTargetProperty(frames, Storyboard.GetTargetProperty(item)); storyboard.Children.Add(frames); ThicknessAnimation animation = item.Clone(); animation.BeginTime = span; Storyboard.SetTarget(animation, controls[i]); storyboard.Children.Add(animation); } } storyboard.FillBehavior = FillBehavior.HoldEnd; storyboard.RepeatBehavior = this.RepeatBehavior; storyboard.Begin(); }