label animation

This commit is contained in:
w-mj committed 2019-04-29 16:31:19 +08:00
1 parent c9d18b31bf
commit 55cb903d1b
3 files changed
+77 -68

No files matched your search

+46 -5
View File
@@ -11,12 +11,20 @@ using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235
namespace FileManager_UWP.Controls {
public class LabelListControl: ItemsControl {
//private int _collpse_distance = 22;
//private List<int> _expanded_position = new List<int>();
private Storyboard _collpse_story_board = new Storyboard();
private Storyboard _expand_story_board = new Storyboard();
private Duration _animate_during = new Duration(TimeSpan.FromMilliseconds(500));
private ResourceDictionary genericResourceDictionary;
public LabelListControl() {
this.DefaultStyleKey = typeof(LabelListControl);
@@ -27,6 +35,10 @@ namespace FileManager_UWP.Controls {
private void ItemsSource_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
update();
}
private void update() {
TextBlock textBlock = (TextBlock)GetTemplateChild("LabelTextBlock");
textBlock.Text = "测试自定义控件\n";
//listView.ItemsSource = ItemsSource;
@@ -36,20 +48,38 @@ namespace FileManager_UWP.Controls {
Canvas labelListCanvas = (Canvas)GetTemplateChild("LabelListCanvas");
//labelListCanvas.Children.Count;
labelListCanvas.Children.Clear();
_collpse_story_board.Children.Clear();
_expand_story_board.Children.Clear();
int expanded_position = 0;
foreach (string label in itemsSource) {
Button b = new Button();
b.Content = label;
b.SetValue(Canvas.LeftProperty, labelListCanvas.Children.Count * 10);
//b.SetValue(Canvas.LeftProperty, labelListCanvas.Children.Count * _collpse_distance);
//b.Style = (Style)Application.Current.Resources["LabelButtonStyle"];
b.SetValue(Canvas.LeftProperty, expanded_position);
b.Style = (Style)genericResourceDictionary["LabelButtonStyle"];
DoubleAnimation collpse_animate = new DoubleAnimation();
collpse_animate.From = labelListCanvas.Children.Count * 22;
collpse_animate.To = expanded_position;
collpse_animate.Duration = _animate_during;
_collpse_story_board.Children.Add(collpse_animate);
Storyboard.SetTarget(collpse_animate, b);
Storyboard.SetTargetProperty(collpse_animate, "(Canvas.Left)");
DoubleAnimation expand_animate = new DoubleAnimation();
expand_animate.From = collpse_animate.To;
expand_animate.To = collpse_animate.From;
expand_animate.Duration = _animate_during;
_expand_story_board.Children.Add(expand_animate);
Storyboard.SetTarget(expand_animate, b);
Storyboard.SetTargetProperty(expand_animate, "(Canvas.Left)");
expanded_position += label.Count() * 14 + 20;
labelListCanvas.Children.Add(b);
}
}
private void update() {
}
private void init() {
var itemsSource = ItemsSource as ObservableCollection<string>;
itemsSource.CollectionChanged += ItemsSource_CollectionChanged;
@@ -59,6 +89,17 @@ namespace FileManager_UWP.Controls {
protected override void OnApplyTemplate() {
init();
update();
}
protected override void OnPointerEntered(PointerRoutedEventArgs e) {
base.OnPointerEntered(e);
_collpse_story_board.Begin();
}
protected override void OnPointerExited(PointerRoutedEventArgs e) {
base.OnPointerExited(e);
_expand_story_board.Begin();
}
}
}
+12 -61
View File
@@ -4,69 +4,20 @@
xmlns:local="using:FileManager_UWP.Controls">
<Style TargetType="Button" x:Key="LabelButtonStyle">
<Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
<Setter Property="BackgroundSizing" Value="OuterBorderEdge" />
<Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
<Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}" />
<Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
<Setter Property="Padding" Value="{StaticResource ButtonPadding}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
<Setter Property="FocusVisualMargin" Value="-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter x:Name="ContentPresenter"
Background="Red"
BackgroundSizing="{TemplateBinding BackgroundSizing}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}"
CornerRadius="{TemplateBinding CornerRadius}"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}" />
</ObjectAnimationUsingKeyFrames>
<PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ContentPresenter>
<Grid x:Name="RootGrid"
CornerRadius="10"
Background="#40FF00"
Padding="2 2 2 2"
BorderThickness="1"
BorderBrush="Blue">
<ContentPresenter
x:Name="ContentPresenter"
Margin="4 4 4 4"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
@@ -78,7 +29,7 @@
<ControlTemplate TargetType="local:LabelListControl">
<StackPanel>
<TextBlock Name="LabelTextBlock"></TextBlock>
<Button Style="{StaticResource LabelButtonStyle}"> HEEEE </Button>
<Button Style="{StaticResource LabelButtonStyle}">HEEEE</Button>
<Canvas Name="LabelListCanvas">
</Canvas>
+19 -2
View File
@@ -22,7 +22,8 @@ namespace FileManager_UWP.View {
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class TestLabelView: Page {
public ObservableCollection<string> Labels = new ObservableCollection<string>{ "miao", "ddd" };
public ObservableCollection<string> Labels = new ObservableCollection<string>{
"照片", "土耳其摔跤", "李若明", "鸢晓曼", "才明洋与吴岳江进行土耳其摔跤", "真正的瑜伽大师"};
public TestLabelView() {
this.InitializeComponent();
@@ -30,7 +31,23 @@ namespace FileManager_UWP.View {
}
private void Button_Click(object sender, RoutedEventArgs e) {
Labels.Add("KKK");
switch (Labels.Count) {
case 0:
Labels.Add("照片");
break;
case 1:
Labels.Add("土耳其摔跤");
break;
case 2:
Labels.Add("李若明");
break;
case 3:
Labels.Add("鸢晓曼");
break;
default:
Labels.Add("才明洋与吴岳江摔跤");
break;
}
Debug.WriteLine(Labels.Count.ToString());
}
}