Last active
August 29, 2015 14:17
-
-
Save kevingosse/350def7c28e48d8b083d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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