Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace KenBurns
{
public sealed partial class SlidingImage2 : UserControl
{
public static DependencyProperty SourceProperty = DependencyProperty.Register(
"Source",
typeof(ImageSource),
typeof(SlidingImage),
new PropertyMetadata(null, (o, e) => ((FrameworkElement)o).InvalidateMeasure()));
public SlidingImage2()
{
this.InitializeComponent();
this.SizeChanged += this.OnSizeChanged;
}
public ImageSource Source
{
get
{
return this.GetValue(SourceProperty) as ImageSource;
}
set
{
this.SetValue(SourceProperty, value);
}
}
protected override Size MeasureOverride(Size availableSize)
{
var height = this.Source == null ? 0 : Math.Min(this.Image.ActualHeight, availableSize.Height);
return new Size(availableSize.Width, height);
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
this.Root.Clip = new RectangleGeometry { Rect = new Rect(new Point(0, 0), e.NewSize) };
this.SetStoryboard();
}
private void OnImageOpened(object sender, RoutedEventArgs e)
{
this.InvalidateMeasure();
this.SetStoryboard();
}
private void SetStoryboard()
{
var storyboard = (Storyboard) this.Resources["SlidingStoryboard"];
if (storyboard.GetCurrentState() != ClockState.Stopped)
{
storyboard.Stop();
storyboard.Seek(TimeSpan.Zero);
}
if (this.Image.ActualHeight > this.ActualHeight)
{
var finalHeight = (this.Image.ActualHeight - this.ActualHeight)*-1;
var animation = (DoubleAnimationUsingKeyFrames) storyboard.Children[0];
animation.KeyFrames.Clear();
var currentTime = TimeSpan.FromSeconds(3);
animation.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(currentTime),
Value = 0
});
currentTime = currentTime.Add(TimeSpan.FromSeconds(finalHeight/-10));
animation.KeyFrames.Add(new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(currentTime),
Value = finalHeight,
EasingFunction = new QuadraticEase {EasingMode = EasingMode.EaseInOut}
});
currentTime = currentTime.Add(TimeSpan.FromSeconds(2));
animation.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(currentTime),
Value = finalHeight
});
currentTime = currentTime.Add(TimeSpan.FromMilliseconds(500));
animation.KeyFrames.Add(new EasingDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(currentTime),
Value = 0,
EasingFunction = new CubicEase {EasingMode = EasingMode.EaseInOut}
});
storyboard.Begin();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment