WPF实现3D翻牌式倒计时特效
作者:RunnerDNA 发布时间:2021-11-11 16:08:21
标签:WPF,翻牌,倒计时
本文实例为大家分享了WPF实现3D翻牌式倒计时的具体代码,供大家参考,具体内容如下
实现效果如下:
思路:使用自定义控件,设置一个背板 MyCardControlBottom,一个卡牌翻动的前部 MyCardControlFront,一个卡牌翻动后的背部 MyCardControlBack,另外实现卡牌翻动的MyCardControl;在主窗体中设置一计时器,根据卡牌上的数字和计时器时间启动翻牌动作。
主要代码:
1、自定义控件MyCardControlBottom
<UserControl x:Class="TurnOverCards.MyCardControlBottom"
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"
x:Name="MyUserControl"
Height="300" Width="200">
<Border BorderThickness="0">
<Border.Effect>
<DropShadowEffect BlurRadius="20" Color="Gray" Direction="-90" ShadowDepth="10"></DropShadowEffect>
</Border.Effect>
<Border.Background>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.75" RadiusY="0.65">
<GradientStop Color="DimGray" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</RadialGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding ElementName=MyUserControl,Path=BottomText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,65,0,2">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<TextBlock Grid.Row="1" Text="{Binding ElementName=MyUserControl,Path=BottomText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,65">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
</Grid>
</Border>
</UserControl>
其中BottomText为自定义属性。
public static readonly DependencyProperty BottomTextProperty = DependencyProperty.Register("BottomText", typeof(string), typeof(MyCardControlBottom), new PropertyMetadata(null));
public string BottomText
{
get { return (string)GetValue(BottomTextProperty); }
set { SetValue(BottomTextProperty, value); }
}
2、自定义控件MyCardControlFront
<UserControl x:Class="TurnOverCards.MyCardControlFront"
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"
x:Name="MyUserControl"
Height="150" Width="200">
<Border BorderThickness="0" ClipToBounds="True">
<Border.Background>
<RadialGradientBrush GradientOrigin="0.5,0.75" Center="0.5,0.5" RadiusX="0.75" RadiusY="0.75">
<GradientStop Color="DimGray" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</RadialGradientBrush>
</Border.Background>
<TextBlock Text="{Binding ElementName=MyUserControl,Path=FrontText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,65,0,2">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
</Border>
</UserControl>
其中FrontText为自定义属性。
3、自定义控件MyCardControlBack
窗体大部分布局与MyCardControlFront 相同,字体部分需要进行翻转显示,其中BackText为自定义属性。
<TextBlock Text="{Binding ElementName=MyUserControl,Path=BackText}" FontFamily="Arial" Foreground="White" FontSize="150" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,-85"
RenderTransformOrigin="0.5,0.5">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="10" Color="Black" Direction="-90" ShadowDepth="2"></DropShadowEffect>
</TextBlock.Effect>
<TextBlock.RenderTransform >
<ScaleTransform ScaleX="-1" ScaleY="-1"/>
</TextBlock.RenderTransform>
</TextBlock>
4、自定义控件MyCardControl
卡牌翻转动作在这里实现。
<UserControl x:Class="TurnOverCards.MyCardControl"
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:TurnOverCards"
x:Name="UserControl"
Loaded="MyUserControl_Loaded">
<Viewport3D Width="200" Height="300">
<Viewport3D.Camera>
<PerspectiveCamera Position="0 -150 480" LookDirection="0 0 -1"/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>
<ModelVisual3D.Content>
<DirectionalLight Color="Transparent"/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ContainerUIElement3D>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="-200 150 0 -200 -150 0 200 -150 0 200 150 0" TriangleIndices="0 1 2 0 2 3" TextureCoordinates="0 0 0 1 1 1 1 0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<local:MyCardControlFront x:Name="frontControl" Width="200" Height="150"/>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
<Viewport2DVisual3D>
<Viewport2DVisual3D.Geometry>
<MeshGeometry3D Positions="200 150 0 200 -150 0 -200 -150 0 -200 150 0" TriangleIndices="0 1 2 0 2 3" TextureCoordinates="0 0 0 1 1 1 1 0"/>
</Viewport2DVisual3D.Geometry>
<Viewport2DVisual3D.Material>
<DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"/>
</Viewport2DVisual3D.Material>
<Viewport2DVisual3D.Visual>
<local:MyCardControlBack x:Name="backControl" Width="200" Height="150"/>
</Viewport2DVisual3D.Visual>
</Viewport2DVisual3D>
<ContainerUIElement3D.Transform>
<Transform3DGroup>
<RotateTransform3D CenterX="0" CenterY="-150" CenterZ="0">
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="MyAxisAngleRotation3D" Angle="0" Axis="1 0 0"/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Transform3DGroup>
</ContainerUIElement3D.Transform>
</ContainerUIElement3D>
</Viewport3D.Children>
</Viewport3D>
</UserControl>
加载时赋值:
public int ShowValue { get; set; }
private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
{
this.frontControl.FrontText = ShowValue.ToString();
}
5、主窗体交互逻辑
private int Count = 10;
private DispatcherTimer frameTimer;
private int TimeValue = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.bottomControl.BottomText = Count.ToString();
for (int i = 1; i <= Count; i++)
{
var card = new MyCardControl();
card.ShowValue = i;
this.mainGrid.Children.Add(card);
Canvas.SetZIndex(card, i);
}
frameTimer = new DispatcherTimer();
frameTimer.Tick += OnFrame;
frameTimer.Interval = TimeSpan.FromSeconds(1);
frameTimer.Start();
}
private void OnFrame(object sender, EventArgs e)
{
if (TimeValue >= Count)
{
if (frameTimer != null)
frameTimer.Stop();
return;
}
if(TimeValue == Count - 1)
{
this.bottomControl.BottomText = 0.ToString();
}
List<MyCardControl> cardList = GetChildObjects<MyCardControl>(this.mainGrid);
foreach (var item in cardList)
{
if(item.ShowValue == Count - TimeValue)
{
Canvas.SetZIndex(item, Count + TimeValue);
DoubleAnimation da = new DoubleAnimation();
da.Duration = new Duration(TimeSpan.FromSeconds(1));
da.To = 180d;
item.ShowValue--;
item.backControl.BackText = item.ShowValue.ToString();
AxisAngleRotation3D aar = item.FindName("MyAxisAngleRotation3D") as AxisAngleRotation3D;
if (aar != null)
aar.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
break;
}
}
TimeValue++;
}
来源:https://blog.csdn.net/dnazhd/article/details/107102763


猜你喜欢
- 目录Java 的Stream流一、定义二、操作的特征三、代码示例1、生成流2、forEach 迭代3、limit方法用于获取指定数量的流4、
- Ribbon 介绍Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负载均衡工具,且已集成在 Eureka 依
- 传统的web项目,只需要在web.xml里配置多个即可,并且支持多个url-pattern在spring boot中,我们默认无需配置,系统
- 一. 概述在开发后端接口, 通常都会涉及检验参数必填校验, 一般我们的处理都是很粗暴的写个if()判断, 然后抛异常. 本文将介绍通过代理的
- 本文实例讲述了Android利用BitMap获得图片像素数据的方法。分享给大家供大家参考,具体如下:网上看到的参考是:int[] pixel
- Android Studio从3.0版本新增了许多功能,当然首当其冲就是从3.0版本新增了对 Kotlin 开发语言的支持,除此之外还有其他
- 本文较为详细的总结分析了Android编程下拉菜单spinner用法。分享给大家供大家参考,具体如下:Spinner控件也是一种列表类型的控
- 目录背景Shutdown Hook 介绍关闭钩子被调用场景注意事项实践Shutdown Hook 在 Spring 中的运用背景如果想在 J
- 简介springmvc对json的前后台传输做了很好封装,避免了重复编码的过程,下面来看看常用的@ResponseBody和@Request
- 我object != null要避免很多NullPointerException。有什么替代方法:if (someobject != nul
- 本篇文章,我们来讲解springcloud的服务注册和发现组件,上一章节我们讲解了如何搭建springcloud的多模块项目,已经新建了sp
- Synchronized实现可见性原理可见性要实现共享变量的可见性,必须保证两点:线程修改后的共享变量值能够及时从工作内存刷新到主内存中其他
- 前提今天在群里聊天的时候有群友问如何捕获错误日志,我说可以自己写,也可以用第三方的比如腾讯的bugly,友盟的错误统计等等,但是那些是别人的
- 最近一段时间 某台服务器上的一个应用总是隔一段时间就自己挂掉 用top看了看 从重新部署应用开始没有多长时间CPU占用上升得很快排查步骤1.
- 问题描述今天在给SpringBoot项目配置 * 的时候发现怎么都进不到 * 的方法里面,在搜索引擎上看了无数篇关于配置 * 的文章都没有找
- 前言大家或许在iOS程序开发中经常遇到屏幕旋转问题,比如说希望指定的页面进行不同的屏幕旋转,但由于系统提供的方法是导航控制器的全局方法,无法
- 先来看看网易云APP的效果:前言关于网易云音乐推荐歌单界面的实现一、实现1.自定义一个圆角图片控件(也可直接使用第三方框架)由于是一些简单的
- sql语句CDATA标签的用法CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data)。
- 1. 介绍这个项目让你可以去读取并解析一个PDF文件,并将其内部结构展示出来. PDF文件的格式标准文档可以从Adobe那儿获取到. 这个项
- C语言是一种高级编程语言,其最重要的特点之一是它允许程序员使用函数来组织代码。函数是一组相关的指令的集合,可以在程序中多次调用。在 C语言中