|
using ClearStyle.ViewModel;
using LinqToVisualTree;
using System;
using System.Linq;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
namespace ClearStyle.Interactions
{
/// <summary>
/// Adds an interaction that allows the user to swipe an item to mark it as complete
/// or delete it
/// </summary>
public class SwipeInteraction : InteractionBase, IInteraction
{
public SwipeInteraction()
{
}
public override void AddElement(FrameworkElement manipulationElement, FrameworkElement transformElement)
{
manipulationElement.ManipulationDelta += Element_ManipulationDelta;
manipulationElement.ManipulationCompleted += Element_ManipulationCompleted;
manipulationElement.ManipulationStarted += Element_ManipulationStarted;
manipulationElement.Tag = transformElement;
}
private void Element_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
if (!IsEnabled)
return;
if (Math.Abs(e.Cumulative.Translation.Y) < Math.Abs(e.Cumulative.Translation.X))
{
IsActive = true;
// initialize the drag
FrameworkElement transformElement = ((FrameworkElement)sender).Tag as FrameworkElement;
transformElement.SetHorizontalOffset(0);
}
}
private void Element_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
if (!IsActive)
return;
FrameworkElement transformElement = ((FrameworkElement)sender).Tag as FrameworkElement;
if (Math.Abs(e.Cumulative.Translation.X) > transformElement.ActualWidth / 3)
{
if (e.Cumulative.Translation.X < 0.0)
{
ToDoItemDeletedAction(transformElement);
}
else
{
ToDoItemCompletedAction(transformElement);
}
}
else
{
ToDoItemBounceBack(transformElement);
}
IsActive = false;
}
private void Element_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (!IsEnabled || !IsActive)
return;
FrameworkElement transformElement = ((FrameworkElement)sender).Tag as FrameworkElement;
// handle the drag to offset the element
double offset = transformElement.GetHorizontalOffset().Value + e.Delta.Translation.X;
transformElement.SetHorizontalOffset(offset);
}
private double TickAndCrossOpacity(double offset)
{
offset = Math.Abs(offset);
if (offset < 50)
return 0;
offset -= 50;
double opacity = offset / 100;
opacity = Math.Max(Math.Min(opacity, 1), 0);
return opacity;
}
private void ToDoItemBounceBack(FrameworkElement fe)
{
var trans = fe.GetHorizontalOffset().Transform;
trans.Animate(trans.X, 0, "X", 300, 0, new BounceEase()
{
Bounciness = 5,
Bounces = 2
});
}
private void ToDoItemDeletedAction(FrameworkElement deletedElement)
{
// find the model object that was deleted
ToDoItem deletedItem = deletedElement.DataContext as ToDoItem;
_todoItems.Remove(deletedItem);
}
private void ToDoItemCompletedAction(FrameworkElement fe)
{
// set the mode object to complete
ToDoItem completedItem = fe.DataContext as ToDoItem;
completedItem.Completed = true;
completedItem.Color = Colors.Green;
// bounce back into place
ToDoItemBounceBack(fe);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.