public void Show() { //2s之内连续按2次,退出 if (clickCount > 0) { if (Completed != null) { Completed(true); } } else { clickCount++; if (Completed != null) { Completed(false); } var tips = new JMessboxControl(); tips.DataContext = this; popup.Height = 65; popup.Width = 200; popup.Margin = new Thickness(140, 380, 0, 0); popup.IsOpen = false; popup.Child = tips; //渐变效果:透明度200毫秒内从0->1 Storyboard story = new Storyboard(); DoubleAnimation topAnimation = new DoubleAnimation(); topAnimation.From = 0; topAnimation.To = 1; topAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200)); Storyboard.SetTarget(topAnimation, tips); Storyboard.SetTargetProperty(topAnimation, "(UIElement.Opacity)"); story.Children.Add(topAnimation); popup.IsOpen = true; story.Begin(); //动画延迟2秒 story.Duration = new Duration(new TimeSpan(0, 0, 2)); //story.BeginTime = new TimeSpan(0, 0, 0, 0, 1); story.Completed += (s1, e1) => { //2s后执行此方法 clickCount = 0; popup.IsOpen = false; story.Stop(); }; } }
/// <summary> /// Fades the element out using a custom DoubleAnimation of the Opacity property. /// </summary> /// <param name="element"></param> /// <param name="duration"></param> /// <param name="easingFunction"> </param> /// <returns></returns> public static async Task FadeOutCustom(this UIElement element, TimeSpan? duration = null, EasingFunctionBase easingFunction = null) { CleanUpPreviousFadeStoryboard(element); var fadeOutStoryboard = new Storyboard(); var fadeOutAnimation = new DoubleAnimation(); if (duration == null) duration = TimeSpan.FromSeconds(0.4); fadeOutAnimation.Duration = duration.Value; fadeOutAnimation.To = 0.0; fadeOutAnimation.EasingFunction = easingFunction; Storyboard.SetTarget(fadeOutAnimation, element); Storyboard.SetTargetProperty(fadeOutAnimation, "Opacity"); fadeOutStoryboard.Children.Add(fadeOutAnimation); SetAttachedFadeStoryboard(element, fadeOutStoryboard); await fadeOutStoryboard.BeginAsync(); element.Opacity = 0.0; fadeOutStoryboard.Stop(); }
/// <summary> /// Fades the element in using the FadeInThemeAnimation. /// </summary> /// <remarks> /// Opacity property of the element is not affected.<br/> /// The duration of the visible animation itself is not affected by the duration parameter. It merely indicates how long the Storyboard will run.<br/> /// If FadeOutThemeAnimation was not used on the element before - nothing will happen.<br/> /// </remarks> /// <param name="element"></param> /// <param name="duration"></param> /// <returns></returns> public static async Task FadeIn(this UIElement element, TimeSpan? duration = null) { // CleanUpPreviousFadeStoryboard(element); ((FrameworkElement)element).Visibility = Visibility.Visible; var fadeInStoryboard = new Storyboard(); var fadeInAnimation = new FadeInThemeAnimation(); if (duration != null) { fadeInAnimation.Duration = duration.Value; } Storyboard.SetTarget(fadeInAnimation, element); fadeInStoryboard.Children.Add(fadeInAnimation); await fadeInStoryboard.BeginAsync(); fadeInStoryboard.Stop(); }
private static void PopupWithAnim() { Storyboard story = new Storyboard(); DoubleAnimation popupUpAnim = CreateDoubleAnimation(_popup.RenderTransform, "Y", new BackEase() { Amplitude = 0.6, EasingMode = EasingMode.EaseOut }, PopupPosition, AnimDuration); story.Children.Add(popupUpAnim); story.Completed +=(s, e)=> { story = new Storyboard(); DoubleAnimation popupDownAnim = CreateDoubleAnimation(_popup.RenderTransform, "Y", new BackEase() { Amplitude = 0.6, EasingMode = EasingMode.EaseIn }, 0, AnimDuration); popupDownAnim.BeginTime = TimeSpan.FromMilliseconds(PopupStayTime); story.Children.Add(popupDownAnim); story.Completed += (s1, e1) => { story.Stop(); Interlocked.Decrement(ref _popupState); Popup(); }; story.Begin(); }; story.Begin(); }
/// <summary> /// Reveals data points using a storyboard. /// </summary> /// <param name="dataPoints">The data points to change the state of. /// </param> /// <param name="dataPointCount">The number of data points in the sequence.</param> /// <param name="newState">The state to change to.</param> private void StaggeredStateChange(IEnumerable<DataPoint> dataPoints, int dataPointCount, DataPointState newState) { if (PlotArea == null || dataPointCount == 0) { return; } Storyboard stateChangeStoryBoard = new Storyboard(); dataPoints.ForEachWithIndex((dataPoint, count) => { // Create an Animation ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames(); objectAnimationUsingKeyFrames.EnableDependentAnimation = true; Storyboard.SetTarget(objectAnimationUsingKeyFrames, dataPoint); Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, "State"); // Create a key frame DiscreteObjectKeyFrame discreteObjectKeyFrame = new DiscreteObjectKeyFrame(); discreteObjectKeyFrame.Value = (object)((int)newState); // Create the specified animation type switch (AnimationSequence) { case AnimationSequence.Simultaneous: discreteObjectKeyFrame.KeyTime = TimeSpan.Zero; break; case AnimationSequence.FirstToLast: discreteObjectKeyFrame.KeyTime = TimeSpan.FromMilliseconds(1000 * ((double)count / dataPointCount)); break; case AnimationSequence.LastToFirst: discreteObjectKeyFrame.KeyTime = TimeSpan.FromMilliseconds(1000 * ((double)(dataPointCount - count - 1) / dataPointCount)); break; } // Add the Animation to the Storyboard objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame); stateChangeStoryBoard.Children.Add(objectAnimationUsingKeyFrames); }); //stateChangeStoryBoard.Duration = new Duration(AnimationSequence.Simultaneous == AnimationSequence ? // TimeSpan.FromTicks(1) : // TimeSpan.FromMilliseconds(1001)); _storyBoardQueue.Enqueue( stateChangeStoryBoard, (sender, args) => { stateChangeStoryBoard.Stop(); }); }
private void PerformRotationChange() { if (_mainButton == null) { return; } double from = LookupFromAngle(); double to = LookupToAngle(); DoubleAnimation d = new DoubleAnimation() { BeginTime = new TimeSpan(0), Duration = new Duration(TimeSpan.FromMilliseconds(450)), From = from, To = to, FillBehavior = FillBehavior.HoldEnd }; Storyboard s = new Storyboard(); s.Children.Add(d); Storyboard.SetTarget(d, _mainButton); Storyboard.SetTargetProperty(d, "(UIElement.RenderTransform).(RotateTransform.Angle)"); s.Begin(); d.Completed += delegate { RotateTransform rTransform = _mainButton.RenderTransform as RotateTransform; if (rTransform != null) { rTransform.Angle = to; } s.Stop(); }; }
private void SearchingFadeIn() { SearchingPanel.Visibility = Visibility.Visible; Storyboard sb = new Storyboard() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; FadeInThemeAnimation fadeAnim = new FadeInThemeAnimation() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; Storyboard.SetTarget(fadeAnim, SearchingPanel); sb.Children.Add(fadeAnim); sb.Completed += delegate { sb.Stop(); }; sb.Begin(); }
private void PivotFadeOut(bool searchFadeIn) { Storyboard sb = new Storyboard() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; FadeOutThemeAnimation fadeAnim = new FadeOutThemeAnimation() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; Storyboard.SetTarget(fadeAnim, PivotControl); sb.Children.Add(fadeAnim); sb.Completed += delegate { PivotControl.Visibility = Visibility.Collapsed; sb.Stop(); if (searchFadeIn) SearchingFadeIn(); }; sb.Begin(); }
private void TypeToSearchFadeOut() { Storyboard sb = new Storyboard() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; FadeOutThemeAnimation fadeAnim = new FadeOutThemeAnimation() { Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; Storyboard.SetTarget(fadeAnim, TypeToSearchPanel); sb.Children.Add(fadeAnim); DoubleAnimation doubleAnim = new DoubleAnimation() { To = 20, From = 0, Duration = new Duration(TimeSpan.FromSeconds(0.5)) }; TranslateTransform translate = new TranslateTransform(); doubleAnim.EnableDependentAnimation = true; TypeToSearchPanel.RenderTransform = translate; Storyboard.SetTarget(doubleAnim, translate); Storyboard.SetTargetProperty(doubleAnim, "Y"); sb.Children.Add(doubleAnim); sb.Completed += delegate { TypeToSearchPanel.Visibility = Visibility.Collapsed; sb.Stop(); }; sb.Begin(); }
/// <summary> /// Runs backward transition. /// </summary> /// <param name="previousPage">The previous page.</param> /// <param name="newPage">The new page.</param> /// <returns>The task that completes when the transition is complete.</returns> /// <exception cref="System.ArgumentNullException"></exception> public async Task TransitionBackward(DependencyObject previousPage, DependencyObject newPage) { if (previousPage == null && newPage == null) { throw new ArgumentNullException("newPage"); } PrepareBackwardAnimations(previousPage, newPage); UpdateTimelineAttributes(); if (previousPage == null) { await BackwardInAnimation.Animate(newPage); } else if (newPage == null) { await BackwardOutAnimation.Animate(previousPage); } else if (this.Mode == PageTransitionMode.Parallel) { var sb = new Storyboard(); Storyboard outSb = null; Storyboard inSb = null; if (this.BackwardOutAnimation != null) { outSb = this.BackwardOutAnimation.GetAnimation(previousPage); sb.Children.Add(outSb); } if (this.BackwardInAnimation != null) { inSb = this.BackwardInAnimation.GetAnimation(newPage); sb.Children.Add(inSb); } await sb.BeginAsync(); sb.Stop(); sb.Children.Clear(); if (this.BackwardOutAnimation != null) { this.BackwardOutAnimation.CleanupAnimation(previousPage, outSb); } if (this.BackwardInAnimation != null) { this.BackwardInAnimation.CleanupAnimation(newPage, inSb); } //await Task.WhenAll( // BackwardOutAnimation.Animate(previousPage), // BackwardInAnimation.Animate(newPage)); } else { if (this.BackwardOutAnimation != null) { await this.BackwardOutAnimation.Animate(previousPage); } if (this.BackwardInAnimation != null) { await this.BackwardInAnimation.Animate(newPage); } } CleanupBackwardAnimations(previousPage, newPage); }
/// <summary> /// Runs the cascades animation. /// </summary> /// <exception cref="System.InvalidOperationException"></exception> public void Cascade() { if (!_isLoaded || _layoutGrid == null) { return; } if (Rows < 1) Rows = 1; if (Columns < 1) Columns = 1; _layoutGrid.Children.Clear(); _layoutGrid.RowDefinitions.Clear(); _layoutGrid.ColumnDefinitions.Clear(); for (int row = 0; row < Rows; row++) { _layoutGrid.RowDefinitions.Add(new RowDefinition()); } for (int column = 0; column < Columns; column++) { _layoutGrid.ColumnDefinitions.Add(new ColumnDefinition()); } var sb = new Storyboard(); var totalDurationInSeconds = RowDelay.TotalSeconds * (Rows - 1) + ColumnDelay.TotalSeconds * (Columns - 1) + TileDuration.TotalSeconds; CascadeDirection direction = this.CascadeDirection; if (direction == CascadeDirection.Random) { direction = (CascadeDirection)Random.Next((int)CascadeDirection.Random); } int startColumn; int exclusiveEndColumn; int columnIncrement; int startRow; int exclusiveEndRow; int rowIncrement; switch (direction) { case CascadeDirection.Shuffle: case CascadeDirection.TopLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.TopRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.BottomRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; case CascadeDirection.BottomLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; default: throw new InvalidOperationException(); } List<Tuple<int, int>> rectCoords = new List<Tuple<int, int>>(Rows * Columns); List<Rectangle> rects = new List<Rectangle>(Rows * Columns); List<PlaneProjection> projs = new List<PlaneProjection>(Rows * Columns); for (int row = startRow; row != exclusiveEndRow; row = row + rowIncrement) for (int column = startColumn; column != exclusiveEndColumn; column = column + columnIncrement) { var rect = new Rectangle(); rects.Add(rect); Grid.SetRow(rect, row); Grid.SetColumn(rect, column); rectCoords.Add(new Tuple<int, int>(column, row)); var brush = new ImageBrush(); brush.ImageSource = this.ImageSource; rect.Fill = brush; var transform = new CompositeTransform(); transform.TranslateX = -column; transform.ScaleX = Columns; transform.TranslateY = -row; transform.ScaleY = Rows; brush.RelativeTransform = transform; var projection = new PlaneProjection(); projection.CenterOfRotationY = 0; rect.Projection = projection; projs.Add(projection); _layoutGrid.Children.Add(rect); } var indices = new List<int>(Rows * Columns); for (int i = 0; i < Rows * Columns; i++) indices.Add(i); if (direction == CascadeDirection.Shuffle) { indices = indices.Shuffle(); } for (int ii = 0; ii < indices.Count; ii++) { var i = indices[ii]; var projection = projs[i]; var rect = rects[i]; var column = rectCoords[ii].Item1; var row = rectCoords[ii].Item2; //Debug.WriteLine("i: {0}, p: {1}, rect: {2}, c: {3}, r: {4}", i, projection.GetHashCode(), rect.GetHashCode(), column, row); var rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, projection); Storyboard.SetTargetProperty(rotationAnimation, "RotationX"); var endKeyTime = this.CascadeSequence == CascadeSequence.EndTogether ? TimeSpan.FromSeconds(totalDurationInSeconds) : TimeSpan.FromSeconds( (double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds + TileDuration.TotalSeconds); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 90 }); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 90 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 0 }); sb.Children.Add(rotationAnimation); var opacityAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(opacityAnimation, rect); Storyboard.SetTargetProperty(opacityAnimation, "Opacity"); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 0 }); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 0 }); opacityAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 1 }); sb.Children.Add(opacityAnimation); } sb.Begin(); sb.Completed += (s, e) => sb.Stop(); }
//a dynamic story board that uses tasks to auto complete themselves when the duration is complete private async Task animateCard(String imageNm, Player p) { Storyboard sb = new Storyboard(); DoubleAnimation da = new DoubleAnimation(); Storyboard.SetTargetProperty(da, "Opacity"); Storyboard.SetTarget(da, srchImg(imageNm, getImgArr(p))); srchImg(imageNm, getImgArr(p)).Source = new BitmapImage(new Uri(base.BaseUri, "/Resources/CardBack.png")); da.From = 0; da.To = 2; da.AutoReverse = true; da.EnableDependentAnimation = true; da.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 50)); sb.Children.Add(da); sb.Begin(); await Task.Delay((int)da.Duration.TimeSpan.TotalMilliseconds); sb.Stop(); }
internal async Task ScrollToVerticalOffsetWithAnimation( double offset, TimeSpan duration, EasingFunctionBase easingFunction) { var sb = new Storyboard(); var da = new DoubleAnimation(); da.EnableDependentAnimation = true; da.From = _scrollViewer.VerticalOffset; da.To = offset; da.EasingFunction = easingFunction; da.Duration = duration; sb.Children.Add(da); Storyboard.SetTarget(sb, _sliderVertical); Storyboard.SetTargetProperty(da, "Value"); await sb.BeginAsync(); sb.Stop(); if (_scrollViewer != null) { _scrollViewer.ScrollToVerticalOffset(offset); } }
private void Flip(Tile tile, Storyboard sb) { sb.Stop(); animating = true; Storyboard.SetTargetName(sb, tile.Rect.Name); if (tile.Faceup) { //getting a color animation from a storyboard *could* be easier... sb.Children.OfType<ColorAnimation>().First().To = Tile.FaceDownColor; } else { sb.Children.OfType<ColorAnimation>().First().To = Tile.FaceUpColor; } //Override the stretch with an actual height to allow the storyboard to run tile.Rect.Height = tile.Rect.ActualHeight; tile.Rect.Width = tile.Rect.ActualWidth; sb.Begin(); }
public async Task TransitionBackward(DependencyObject previousPage, DependencyObject newPage) { if (previousPage == null && newPage == null) { throw new InvalidOperationException(); } if (previousPage == null) { await BackwardInAnimation.Animate(newPage); } else if (newPage == null) { await BackwardOutAnimation.Animate(previousPage); } else if (this.Mode == PageTransitionMode.Parallel) { var sb = new Storyboard(); var outSb = BackwardOutAnimation.GetAnimation(previousPage); var inSb = BackwardInAnimation.GetAnimation(newPage); sb.Children.Add(outSb); sb.Children.Add(inSb); await sb.BeginAsync(); sb.Stop(); sb.Children.Clear(); //await Task.WhenAll( // BackwardOutAnimation.Animate(previousPage), // BackwardInAnimation.Animate(newPage)); } else { await BackwardOutAnimation.Animate(previousPage); await BackwardInAnimation.Animate(newPage); } }
private void UpdatePosition(bool isShowingComments) { double to = 0.0; double from = 0.0; if (isShowingComments && !_isShowingComments) { to = -_translateAmount; } else if (!isShowingComments && _isShowingComments) { from = -_translateAmount; } if (isShowingComments != _isShowingComments) { DoubleAnimation d = new DoubleAnimation() { BeginTime = new TimeSpan(0), Duration = new Duration(TimeSpan.FromMilliseconds(400)), From = from, To = to, FillBehavior = FillBehavior.HoldEnd, EasingFunction = new QuadraticEase() }; Storyboard s = new Storyboard(); s.Children.Add(d); Storyboard.SetTarget(d, ChildGrid); Storyboard.SetTargetProperty(d, "(UIElement.RenderTransform).(TranslateTransform.Y)"); s.Begin(); d.Completed += delegate { if (isShowingComments) { ((TranslateTransform)ChildGrid.RenderTransform).Y = -_translateAmount; } else { ((TranslateTransform)ChildGrid.RenderTransform).Y = 0; } s.Stop(); }; _isShowingComments = !_isShowingComments; } }