Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingosse/350def7c28e48d8b083d to your computer and use it in GitHub Desktop.
Save kevingosse/350def7c28e48d8b083d to your computer and use it in GitHub Desktop.
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