Skip to content

Instantly share code, notes, and snippets.

@Injac
Created October 27, 2014 23:40
Show Gist options
  • Save Injac/19053698b0fcf791826f to your computer and use it in GitHub Desktop.
Save Injac/19053698b0fcf791826f to your computer and use it in GitHub Desktop.
FlipView Animation Behavior Anitmate a FlipViewControl with fadein or a slide-animation (botton to top)
public class FlipViewAnimationBehaviour: DependencyObject, IBehavior {
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public DependencyObject AssociatedObject {
get;
set;
}
private SlideAnimation animation;
private Storyboard currentAnimation;
public SlideAnimation Animation {
get {
return animation;
}
set {
animation = value;
this.SetAnimation(animation);
}
}
private void SetAnimation(SlideAnimation animation) {
this.animations = new FlipViewAnimations();
this.animations.RecreateAnimations();
if(animation == SlideAnimation.FadinAnimation) {
this.currentAnimation = this.animations.FadeInAnimation;
}
if(animation == SlideAnimation.SlideInBottomTop) {
this.currentAnimation = this.animations.SlideInLeftAnimation;
}
}
private FlipViewAnimations animations;
protected virtual void OnAttached() {
(AssociatedObject as FlipView).SelectionChanged += AssociatedObject_SelectionChanged;
//this.currentAnimation = animations.FadeInAnimation;
}
void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var flipView = sender as FlipView;
if (e.AddedItems.Count > 0) {
FlipViewItem initem = flipView.ContainerFromItem(e.AddedItems[0]) as FlipViewItem;
if (initem != null) {
this.SetAnimation(this.animation);
if (this.animation == SlideAnimation.SlideInBottomTop) {
initem.RenderTransform = new TranslateTransform();
Storyboard.SetTarget(this.currentAnimation, initem.RenderTransform);
}
else {
Storyboard.SetTarget(this.currentAnimation, initem);
}
this.currentAnimation.Begin();
}
}
}
protected virtual void OnDetaching() {
(AssociatedObject as FlipView).SelectionChanged -= AssociatedObject_SelectionChanged;
this.animations = null;
}
public void Attach(DependencyObject associatedObject) {
this.AssociatedObject = associatedObject;
OnAttached();
}
public void Detach() {
OnDetaching();
}
}
public enum SlideAnimation {
FadinAnimation,
SlideInBottomTop
}
public class FlipViewAnimations {
public Storyboard FadeInAnimation {
get;
set;
}
public Storyboard SlideInLeftAnimation {
get;
set;
}
public FlipViewAnimations() {
CreateFadeInAnimation();
CreateSlideInBottomTop();
}
public void RecreateAnimations() {
CreateFadeInAnimation();
CreateSlideInBottomTop();
}
private void CreateFadeInAnimation() {
FadeInAnimation = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.From = 0.0;
da.To = 1.0;
da.AutoReverse = false;
Duration dur = new Duration(TimeSpan.FromMilliseconds(900));
da.Duration = dur;
FadeInAnimation.Children.Add(da);
Storyboard.SetTargetProperty(FadeInAnimation, "Opacity");
}
private void CreateSlideInBottomTop() {
SlideInLeftAnimation = new Storyboard();
DoubleAnimation da = new DoubleAnimation();
da.From = 500;
da.To = 0;
da.AutoReverse = false;
Duration dur = new Duration(TimeSpan.FromMilliseconds(600));
da.Duration = dur;
SlideInLeftAnimation.Children.Add(da);
Storyboard.SetTargetProperty(da, "Y");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment