WPF数字滚动效果

分手后的思念是犯贱 2022-01-05 04:05 418阅读 0赞

和WPF数字滚动抽奖有区别,WPF数字滚动抽奖是随机的,而这里是确定的。

为了系统演示,这个效果通宵加班写了整整6个小时,中间就上了次厕所。

代码:

RollingNumberItemCtrl.xaml代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. <UserControl x:Class="SunCreate.Common.Controls.RollingNumberItemCtrl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="50" d:DesignWidth="300">
  8. <Grid>
  9. <Grid Height="{Binding Height}" Width="{Binding Width}" ClipToBounds="True">
  10. <StackPanel x:Name="stackPanel" Width="{Binding Width}" HorizontalAlignment="Center" Margin="{Binding MarginTop}" >
  11. <Border Width="{Binding Width}" Height="{Binding Height}">
  12. <TextBlock FontSize="{Binding Height}" Text="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  13. </Border>
  14. <Border Width="{Binding Width}" Height="{Binding Height}">
  15. <TextBlock FontSize="{Binding Height}" Text="1" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  16. </Border>
  17. <Border Width="{Binding Width}" Height="{Binding Height}">
  18. <TextBlock FontSize="{Binding Height}" Text="2" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  19. </Border>
  20. <Border Width="{Binding Width}" Height="{Binding Height}">
  21. <TextBlock FontSize="{Binding Height}" Text="3" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  22. </Border>
  23. <Border Width="{Binding Width}" Height="{Binding Height}">
  24. <TextBlock FontSize="{Binding Height}" Text="4" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  25. </Border>
  26. <Border Width="{Binding Width}" Height="{Binding Height}">
  27. <TextBlock FontSize="{Binding Height}" Text="5" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  28. </Border>
  29. <Border Width="{Binding Width}" Height="{Binding Height}">
  30. <TextBlock FontSize="{Binding Height}" Text="6" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  31. </Border>
  32. <Border Width="{Binding Width}" Height="{Binding Height}">
  33. <TextBlock FontSize="{Binding Height}" Text="7" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  34. </Border>
  35. <Border Width="{Binding Width}" Height="{Binding Height}">
  36. <TextBlock FontSize="{Binding Height}" Text="8" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  37. </Border>
  38. <Border Width="{Binding Width}" Height="{Binding Height}">
  39. <TextBlock FontSize="{Binding Height}" Text="9" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  40. </Border>
  41. <Border Width="{Binding Width}" Height="{Binding Height}">
  42. <TextBlock FontSize="{Binding Height}" Text="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  43. </Border>
  44. <Border Width="{Binding Width}" Height="{Binding Height}">
  45. <TextBlock FontSize="{Binding Height}" Text="1" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  46. </Border>
  47. <Border Width="{Binding Width}" Height="{Binding Height}">
  48. <TextBlock FontSize="{Binding Height}" Text="2" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  49. </Border>
  50. <Border Width="{Binding Width}" Height="{Binding Height}">
  51. <TextBlock FontSize="{Binding Height}" Text="3" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  52. </Border>
  53. <Border Width="{Binding Width}" Height="{Binding Height}">
  54. <TextBlock FontSize="{Binding Height}" Text="4" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  55. </Border>
  56. <Border Width="{Binding Width}" Height="{Binding Height}">
  57. <TextBlock FontSize="{Binding Height}" Text="5" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  58. </Border>
  59. <Border Width="{Binding Width}" Height="{Binding Height}">
  60. <TextBlock FontSize="{Binding Height}" Text="6" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  61. </Border>
  62. <Border Width="{Binding Width}" Height="{Binding Height}">
  63. <TextBlock FontSize="{Binding Height}" Text="7" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  64. </Border>
  65. <Border Width="{Binding Width}" Height="{Binding Height}">
  66. <TextBlock FontSize="{Binding Height}" Text="8" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  67. </Border>
  68. <Border Width="{Binding Width}" Height="{Binding Height}">
  69. <TextBlock FontSize="{Binding Height}" Text="9" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
  70. </Border>
  71. </StackPanel>
  72. </Grid>
  73. </Grid>
  74. </UserControl>

RollingNumberItemCtrl.xaml.cs代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Animation;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace SunCreate.Common.Controls
  17. {
  18. /// <summary>
  19. /// NumCtrl.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class RollingNumberItemCtrl : INotifyPropertyChanged
  22. {
  23. private Thickness _MarginTop = new Thickness(0, 0, 0, 0);
  24. /// <summary>
  25. /// Margin
  26. /// </summary>
  27. public Thickness MarginTop
  28. {
  29. get { return _MarginTop; }
  30. set
  31. {
  32. _MarginTop = value;
  33. OnPropertyChanged("MarginTop");
  34. }
  35. }
  36. private double _Num;
  37. /// <summary>
  38. /// 数字
  39. /// </summary>
  40. public double Num
  41. {
  42. get { return _Num; }
  43. set
  44. {
  45. var ease = new ExponentialEase()
  46. {
  47. EasingMode = EasingMode.EaseOut,
  48. };
  49. ThicknessAnimation animation = new ThicknessAnimation();
  50. animation.EasingFunction = ease;
  51. animation.From = new Thickness(0, 0 - _Num * Height, 0, 0);
  52. double top1 = MarginTop.Top;
  53. double d = 0 - value * Height;
  54. if (value < _Num)
  55. {
  56. d = 0 - (value + 10) * Height;
  57. }
  58. _Num = value;
  59. OnPropertyChanged("Num");
  60. MarginTop = new Thickness(0, d, 0, 0);
  61. double top2 = MarginTop.Top;
  62. animation.To = MarginTop;
  63. //animation.Duration = TimeSpan.FromMilliseconds((top1 - top2) * 10);
  64. if (top1 != top2)
  65. {
  66. animation.Duration = TimeSpan.FromMilliseconds(1500);
  67. this.stackPanel.BeginAnimation(StackPanel.MarginProperty, animation);
  68. }
  69. MarginTop = new Thickness(0, 0 - value * Height, 0, 0);
  70. }
  71. }
  72. public RollingNumberItemCtrl()
  73. {
  74. InitializeComponent();
  75. this.DataContext = this;
  76. }
  77. #region INotifyPropertyChanged接口
  78. public event PropertyChangedEventHandler PropertyChanged;
  79. protected void OnPropertyChanged(string name)
  80. {
  81. if (PropertyChanged != null)
  82. {
  83. PropertyChanged(this, new PropertyChangedEventArgs(name));
  84. }
  85. }
  86. #endregion
  87. }
  88. }

RollingNumberCtrl.xaml代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. <UserControl x:Class="SunCreate.Common.Controls.RollingNumberCtrl"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. xmlns:local="clr-namespace:SunCreate.Common.Controls"
  8. d:DesignHeight="30" d:DesignWidth="300" Loaded="UserControl_Loaded">
  9. <Grid>
  10. <StackPanel x:Name="stackPanel" Orientation="Horizontal" >
  11. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  12. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  13. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  14. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  15. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  16. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  17. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  18. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
  19. <local:RollingNumberItemCtrl Height="30" Width="18" Num="0"></local:RollingNumberItemCtrl>
  20. </StackPanel>
  21. </Grid>
  22. </UserControl>

RollingNumberCtrl.xaml.cs代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace SunCreate.Common.Controls
  17. {
  18. /// <summary>
  19. /// MyTextBlock.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class RollingNumberCtrl : INotifyPropertyChanged
  22. {
  23. private bool _firstLoaded = true;
  24. public double ItemHeight
  25. {
  26. get { return (double)this.GetValue(RollingNumberCtrl.ItemHeightProperty); }
  27. set
  28. { this.SetValue(RollingNumberCtrl.ItemHeightProperty, value); }
  29. }
  30. private static DependencyProperty ItemHeightProperty = DependencyProperty.Register("ItemHeight", typeof(double), typeof(RollingNumberCtrl));
  31. public string NumStr
  32. {
  33. get { return (string)this.GetValue(RollingNumberCtrl.NumStrProperty); }
  34. set
  35. { this.SetValue(RollingNumberCtrl.NumStrProperty, value); }
  36. }
  37. private static DependencyProperty NumStrProperty = DependencyProperty.Register("NumStr", typeof(string), typeof(RollingNumberCtrl), new PropertyMetadata(null, new PropertyChangedCallback(NumStrChanged)));
  38. private static void NumStrChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  39. {
  40. (sender as RollingNumberCtrl).UpdateNumStr((sender as RollingNumberCtrl).NumStr);
  41. }
  42. private void UpdateNumStr(string numStr)
  43. {
  44. Text = numStr;
  45. }
  46. private string _Text;
  47. /// <summary>
  48. /// 文本
  49. /// </summary>
  50. public string Text
  51. {
  52. get { return _Text; }
  53. set
  54. {
  55. _Text = value;
  56. OnPropertyChanged("Text");
  57. if (!_firstLoaded)
  58. {
  59. RollingNumberItemCtrl[] numArr = new RollingNumberItemCtrl[stackPanel.Children.Count];
  60. int index = 1;
  61. foreach (RollingNumberItemCtrl num in stackPanel.Children)
  62. {
  63. numArr[numArr.Length - index++] = num;
  64. }
  65. if (_Text != null)
  66. {
  67. int i = 0;
  68. foreach (char c in _Text.Reverse())
  69. {
  70. double d = Convert.ToInt32(c - 48); ;
  71. numArr[i++].Num = d;
  72. }
  73. for (int k = 0; k < i; k++)
  74. {
  75. numArr[k].Visibility = Visibility.Visible;
  76. }
  77. for (int k = i; k < numArr.Length; k++)
  78. {
  79. numArr[k].Visibility = Visibility.Collapsed;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. public RollingNumberCtrl()
  86. {
  87. InitializeComponent();
  88. }
  89. #region INotifyPropertyChanged接口
  90. public event PropertyChangedEventHandler PropertyChanged;
  91. protected void OnPropertyChanged(string name)
  92. {
  93. if (PropertyChanged != null)
  94. {
  95. PropertyChanged(this, new PropertyChangedEventArgs(name));
  96. }
  97. }
  98. #endregion
  99. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  100. {
  101. if (_firstLoaded) _firstLoaded = false;
  102. foreach (RollingNumberItemCtrl num in stackPanel.Children)
  103. {
  104. num.Height = this.ItemHeight;
  105. num.Width = this.ItemHeight * 0.6;
  106. num.FontWeight = this.FontWeight;
  107. }
  108. Text = NumStr;
  109. }
  110. }
  111. }

如何使用:

ContractedBlock.gif ExpandedBlockStart.gif

  1. <controls:RollingNumberCtrl Margin="0 2 0 0" ItemHeight="20" NumStr="{Binding CarInOut}" Foreground="#fff" FontSize="20" FontWeight="Bold" ></controls:RollingNumberCtrl>

效果图:

174862-20190402055122936-2088691778.gif

转载于:https://www.cnblogs.com/s0611163/p/10640275.html

发表评论

表情:
评论列表 (有 0 条评论,418人围观)

还没有评论,来说两句吧...

相关阅读