WPF实现倒计时转场动画效果

代码如下

一、创建 CountdownTimer.xaml 继承ContentControl代码如下。

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace WPFDevelopers.Controls
{
    public enum CountdownTimerEffect
    {
        Default,
        MultiColor
    }
    public class CountdownTimer : ContentControl
    {
        private Storyboard storyboard;
        private const double seconds = 800;
        private double currentSeconds = seconds;
        private Grid myGrid;
        public int Number
        {
            get { return (int)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }

        public static readonly DependencyProperty NumberProperty =
        DependencyProperty.Register("Number", typeof(int), typeof(CountdownTimer), new PropertyMetadata(3));

        /// <summary>
        /// 完成后回到开始
        /// </summary>
        public bool IsFinishStart
        {
            get { return (bool)GetValue(IsFinishStartProperty); }
            set { SetValue(IsFinishStartProperty, value); }
        }

        public static readonly DependencyProperty IsFinishStartProperty =
            DependencyProperty.Register("IsFinishStart", typeof(bool), typeof(CountdownTimer), new PropertyMetadata(false));

        public CountdownTimerEffect CountdownTimerEffect
        {
            get { return (CountdownTimerEffect)GetValue(CountdownTimerEffectProperty); }
            set { SetValue(CountdownTimerEffectProperty, value); }
        }

        public static readonly DependencyProperty CountdownTimerEffectProperty =
            DependencyProperty.Register("ExhibitionEnum", typeof(CountdownTimerEffect), typeof(CountdownTimer), new PropertyMetadata(CountdownTimerEffect.Default));

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            NameScope.SetNameScope(this, new NameScope());
            if (FontSize == SystemFonts.CaptionFontSize)
                FontSize = 200;
            FontFamily = DrawingContextHelper.FontFamily;
            storyboard = new Storyboard();
            myGrid = new Grid();
            myGrid.Name = "myGrid";
            myGrid.ToolTip = "MouseDown";
            myGrid.Background = new SolidColorBrush(Colors.White);
            var linearGradient = new LinearGradientBrush
            {
                GradientStops = new GradientStopCollection
                {
                   new GradientStop{ Color = Colors.Red, Offset = 1 },
                   new GradientStop{ Color = Colors.White, Offset = 1 },
                   new GradientStop{ Color = Colors.White, Offset = .5 },
                   new GradientStop{ Color = Colors.Red, Offset = .5 },
                   new GradientStop{ Color = Colors.Red, Offset = 0 },
                   new GradientStop{ Color = Colors.White, Offset = 0 },
                },
                StartPoint = new Point(0.5, 0),
                EndPoint = new Point(10, 10),
                SpreadMethod = GradientSpreadMethod.Reflect,
                MappingMode = BrushMappingMode.Absolute
            };
            SolidColorBrush solidColor;
            this.RegisterName(myGrid.Name, myGrid);
            var num = 0;
            for (int i = Number; i >= num; i--)
            {
                var textBlock = new TextBlock();
                switch (CountdownTimerEffect)
                {
                    case CountdownTimerEffect.Default:
                        if (i % 2 == 0)
                            solidColor = Brushes.White;
                        else
                            solidColor = Brushes.Black;
                        textBlock.Foreground = solidColor;
                        break;
                    case CountdownTimerEffect.MultiColor:
                        textBlock.Foreground = linearGradient;
                        break;

                }

                textBlock.Text = i.ToString();
                textBlock.Name = $"textBlock{i}";
                textBlock.FontSize = FontSize;
                textBlock.FontWeight = FontWeights.ExtraBold;
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                textBlock.HorizontalAlignment = HorizontalAlignment.Center;
                textBlock.RenderTransformOrigin = new Point(.5, .5);
                textBlock.Effect = new DropShadowEffect
                {
                    ShadowDepth = 2,
                    RenderingBias = RenderingBias.Performance,
                    Color = Colors.Red
                };
                if (!i.Equals(Number))
                    textBlock.Opacity = 0;
                textBlock.RenderTransform = new ScaleTransform
                {
                    ScaleX = 2,
                    ScaleY = 2,
                };
                this.RegisterName(textBlock.Name, textBlock);

                TimeSpan beginTime = TimeSpan.Zero;
                if (storyboard.Children.Count > 0)
                {
                    beginTime = TimeSpan.FromMilliseconds(currentSeconds);
                    currentSeconds += seconds;
                }
                var cubicEase = new CubicEase
                {
                    EasingMode = EasingMode.EaseIn,
                };
                DoubleAnimation doubleAnimationScaleX = new DoubleAnimation();
                doubleAnimationScaleX.From = 2;
                doubleAnimationScaleX.To = 0;
                doubleAnimationScaleX.EasingFunction = cubicEase;

                Storyboard.SetTargetName(doubleAnimationScaleX, textBlock.Name);
                Storyboard.SetTargetProperty(doubleAnimationScaleX, new PropertyPath("(TextBlock.RenderTransform).(ScaleTransform.ScaleX)"));

                var doubleAnimationScaleY = new DoubleAnimation
                {
                    From = 2,
                    To = 0,
                    EasingFunction = cubicEase
                };
                Storyboard.SetTargetName(doubleAnimationScaleY, textBlock.Name);
                Storyboard.SetTargetProperty(doubleAnimationScaleY, new PropertyPath("(TextBlock.RenderTransform).(ScaleTransform.ScaleY)"));

                doubleAnimationScaleX.BeginTime = beginTime;
                doubleAnimationScaleY.BeginTime = beginTime;
                doubleAnimationScaleX.Duration = TimeSpan.FromMilliseconds(seconds);
                doubleAnimationScaleY.Duration = TimeSpan.FromMilliseconds(seconds);
                if (!i.Equals(Number))
                {
                    var doubleAnimationOpacity = new DoubleAnimation
                    {
                        Duration = TimeSpan.FromMilliseconds(0),
                        BeginTime = beginTime,
                        From = 0,
                        To = 1
                    };
                    Storyboard.SetTargetName(doubleAnimationOpacity, textBlock.Name);
                    Storyboard.SetTargetProperty(doubleAnimationOpacity, new PropertyPath(TextBlock.OpacityProperty));
                    storyboard.Children.Add(doubleAnimationOpacity);
                }

                if (i % 2 == 0)
                {
                    var colorAnimation = new ColorAnimation
                    {
                        Duration = TimeSpan.FromMilliseconds(0),
                        From = Colors.White,
                        BeginTime = beginTime,
                        To = Colors.Black
                    };
                    Storyboard.SetTargetName(colorAnimation, myGrid.Name);
                    Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"));
                    storyboard.Children.Add(colorAnimation);
                }
                else
                {
                    if (!i.Equals(Number))
                    {
                        var colorAnimation = new ColorAnimation
                        {
                            Duration = TimeSpan.FromMilliseconds(0),
                            BeginTime = beginTime,
                            From = Colors.Black,
                            To = Colors.White
                        };
                        Storyboard.SetTargetName(colorAnimation, myGrid.Name);
                        Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("(Panel.Background).(SolidColorBrush.Color)"));
                        storyboard.Children.Add(colorAnimation);
                    }
                }

                storyboard.Children.Add(doubleAnimationScaleX);
                storyboard.Children.Add(doubleAnimationScaleY);

                myGrid.Children.Add(textBlock);
            }
            this.Content = myGrid;

        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            if (storyboard != null && storyboard.Children.Count > 0)
            {
                storyboard.Completed += (s, y) =>
                {
                    myGrid.Background = new SolidColorBrush(Colors.White);
                    if (IsFinishStart)
                    {
                        var scaleTransform = new ScaleTransform
                        {
                            ScaleX = 2,
                            ScaleY = 2
                        };
                        var tb = myGrid.Children.Cast<TextBlock>().First();
                        tb.RenderTransform = scaleTransform;
                    }
                };
                storyboard.Begin(this);

            }

        }
    }
}

二、CountdownTimerExample.xaml 代码如下

<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.CountdownTimerExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
             xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
   
    <Grid Margin="10" Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border Margin="0,0,0,0" Background="{StaticResource WhiteSolidColorBrush}" CornerRadius="4,4,0,0" 
                    Effect="{StaticResource NormalShadowDepth}">
            <wpfdev:NavigateMenu TabStripPlacement="Top" SelectionChanged="NavigateMenu_SelectionChanged">
                <ListBoxItem Content="Default"/>
                <ListBoxItem Content="MultiColor"/>
            </wpfdev:NavigateMenu>
        </Border>
        <Border Grid.Row="1" Background="{StaticResource WhiteSolidColorBrush}" CornerRadius="0,0,4,4"
                    Effect="{StaticResource NormalShadowDepth}">
            <Grid Margin="10">
                <wpfdev:CountdownTimer Number="3" x:Name="CountdownTimer1"/>
                <UniformGrid Columns="4" Visibility="Collapsed" x:Name="CountdownTimerGroup">
                    <wpfdev:CountdownTimer Number="9" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/>
                    <wpfdev:CountdownTimer Number="5" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/>
                    <wpfdev:CountdownTimer Number="2" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/>
                    <wpfdev:CountdownTimer Number="7" CountdownTimerEffect="MultiColor" FontSize="150" IsFinishStart="True"/>
                </UniformGrid>
            </Grid>
        </Border>
    </Grid>
</UserControl>

三、CountdownTimerExample.xaml.cs 代码如下

using System.Windows;
using System.Windows.Controls;

namespace WPFDevelopers.Samples.ExampleViews
{
    /// <summary>
    /// CountdownTimerExample.xaml 的交互逻辑
    /// </summary>
    public partial class CountdownTimerExample : UserControl
    {
        public CountdownTimerExample()
        {
            InitializeComponent();
        }

        private void NavigateMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = e.AddedItems[0] as ListBoxItem;
            if (item == null) return;
            switch (item.Content.ToString())
            {
                case "Default":
                    if(CountdownTimer1.Visibility != Visibility.Visible)
                    {
                        CountdownTimer1.Visibility = Visibility.Visible;
                        CountdownTimerGroup.Visibility = Visibility.Collapsed;
                    }
                    break;
                case "MultiColor":
                    if (CountdownTimerGroup.Visibility != Visibility.Visible)
                    {
                        CountdownTimerGroup.Visibility = Visibility.Visible;
                        CountdownTimer1.Visibility = Visibility.Collapsed;
                    }
                    break;
            }
        }
    }
}

效果预览

到此这篇关于WPF实现倒计时转场动画效果的文章就介绍到这了,更多相关WPF倒计时动画内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • WPF实现文字粒子闪烁动画效果

    本文实例为大家分享了WPF实现文字粒子闪烁动画的具体代码,供大家参考,具体内容如下 实现效果如下: 思路:首先根据显示文本创建文本路径Geometry,然后在路径内随机生成圆形粒子并添加动画. 步骤: 1.粒子类Particle.cs public class Particle { /// <summary> /// 形状 /// </summary> public Ellipse Shape; /// <summary> /// 坐标 /// </summary

  • 基于WPF实现一个简单的音频播放动画控件

    目录 1.实现代码 2.效果预览 1.实现代码 一.创建AnimationAudio.xaml代码如下 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    

  • WPF实现动画效果

    学习平台 微软开发者博客:https://devblogs.microsoft.com/?WT.mc_id=DT-MVP-5003986微软文档与学习:https://docs.microsoft.com/zh-cn/?WT.mc_id=DT-MVP-5003986微软开发者平台:https://developer.microsoft.com/en-us/?WT.mc_id=DT-MVP-5003986 1.介绍 在之前做winform中, 也做过一些动画效果, 但是整个动画都需要我们自己去编写

  • WPF实现画线动画效果

    本文实例为大家分享了WPF实现画线动画的具体代码,供大家参考,具体内容如下 需求:一条直线(不是曲线),模范笔画一样在画布上逐渐画出来.但前提是,用后台代码实现,并非WPF标签 效果: 上代码: /// <summary> /// Window2.xaml 的交互逻辑 /// </summary> public partial class Window2 : Window { public Window2() { InitializeComponent(); var canvas

  • WPF实现流光动画特效

    一.代码 <Window.Resources> <!--外--> <Storyboard x:Key="Storyboard1" RepeatBehavior="Forever"> <PointAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Stroke).(LinearGradientBrush.StartPoint)" Storybo

  • WPF实现左右移动(晃动)动画效果

    本文实例为大家分享了WPF实现左右移动效果展示的具体代码,供大家参考,具体内容如下 实现控件或布局的左右移动(晃动)主要用到DoubleAnimation以及Storyboard 布局代码为: <Canvas> <Grid Width="200" Height="100" Background="MediumAquamarine" Name="GroupboxArea" Canvas.Left="1

  • WPF实现倒计时转场动画效果

    代码如下 一.创建 CountdownTimer.xaml 继承ContentControl代码如下. using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windo

  • vue router自动判断左右翻页转场动画效果

    前段时间做了一个移动端spa项目,技术基于 :vue + vue-router + vuex + mint-ui 因为使用了vue-cli脚手架的webpack模版,所有页面都以.vue为后缀的文件作为一个组件 最近公司项目比较少终于有空来记录一下自己对vue-router的一些小小的使用心得, 一般的移动端口单页应用在跳转页面时候会有相应的转场动画,比如: 1. 从当前一级页面跳转二级页面需要展示的转场动画是一级页面向屏幕左边移动消失的同时, 二级页面从屏幕的右边向左边移动出现.(类似翻书翻到

  • Android Flutter实现页面切换转场动画效果

    目录 前言 Hero 动画过程 Hero 基础示例 总结 前言 写了一篇基础的性能优化的内容,继续我们的动画相关的介绍.今天的主角是英雄 —— Hero 组件.Hero 组件非常适合从列表.概览页切换到详情页转场动画场合.因为可以将两个页面的组件串起来动画,体验上会觉得整个操作的连贯性非常好.下面是我们这篇要做的一个效果. 屏幕录制2021-11-09 下午9.39.49.gif Hero 动画过程 Hero 本质是是在不同的路由页面做了一个中转层,然后通过动画完成过渡,下面用4张图是官方演示的

  • Vue实现Tab标签路由效果并用Animate.css做转场动画效果的代码第1/3页

    类似于浏览器窗口一样的路由切换逻辑,看着还是挺高大上的,本以为有很多高级的玩意儿,奈何复杂的东西总是由简单的东西拼接而成的,这个功能也不例外. 本篇文章主要描述两个问题: 如何实现这种Tab标签页的路由效果 如何为路由切换添加转场动画. 该功能的开发主要使用到 AntDesignVue 组件库的Tab组件和 Animate.css 效果如下: Tab标签页实现 首先是该组件的模板部分, ContextMenu 组件是我们自定义的右键菜单,后面会说到. a-tabs 组件则是 ant 的组件,具体

  • IOS实战之自定义转场动画详解

    转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的动画,这些效果如果不用自定义动

  • 详解IOS图层转场动画

    CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比) 具体代码: /* 过渡效果 fade //交叉淡化过渡(不支持过渡方

  • 实例讲解iOS中的CATransition转场动画使用

    一.简介 CATransition是CAAnimation的子类,用于做转场动画 能够为图层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 如:UINavigationController导航控制器就是通过CATransition转场动画实现了将控制器的视图推入屏幕的动画效果 CATransition头文件 动画属性: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点

  • IOS轻松几步实现自定义转场动画

    一.系统提供的转场动画 目前,系统给我们提供了push/pops和present/dismiss两种控制器之间跳转方.当然,通过设置UIModalTransitionStyle属性,可以实现下面4种modal效果,相信大家都比较熟悉了,这里就不再展示效果图. UIModalTransitionStyleCoverVertical // 从下往上, UIModalTransitionStyleFlipHorizontal // 水平翻转 UIModalTransitionStyleCrossDis

  • 详解iOS开发中的转场动画和组动画以及UIView封装动画

    一.转场动画 CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果 属性解析: type:动画过渡类型 subtype:动画过渡方向 startProgress:动画起点(在整体动画的百分比) endProgress:动画终点(在整体动画的百分比) 转场动画代码示例 1.界面搭建 2.实现代码 复制代码

随机推荐