Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps / Windows Phone 7

Clearer – A Gesture-Driven Windows 8 To-Do Application

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
9 Oct 2012CPOL15 min read 52.5K   878   22  
This article describes my experiences of porting a novel gesture-driven Windows Phone app to Windows 8.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions