日期:2014-05-17  浏览次数:20716 次

windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放

实现方法:


 

对Manipulation进行抽象化 使不同容器可共用多点缩放事件,

C# 代码如下:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
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 MetroTimeline
{
    public class MetroManipulationHelper
    {
        /// <summary>
        /// 发生碰撞时的操作方法库
        /// </summary>
        public static Dictionary<Predicate<string>, Action<ManipulationDeltaRoutedEventArgs>>
            BoundaryFeedbackDict = new Dictionary<Predicate<string>, Action<ManipulationDeltaRoutedEventArgs>>();

        /// <summary>
        /// 多点触控开始后方法库
        /// </summary>
        public static Dictionary<Predicate<string>, Action<ManipulationStartedRoutedEventArgs>>
            ManipulationStartedDict = new Dictionary<Predicate<string>, Action<ManipulationStartedRoutedEventArgs>>();

        /// <summary>
        /// 多点触控完成后方法库
        /// </summary>
        public static Dictionary<Predicate<string>, Action<ManipulationCompletedRoutedEventArgs>>
            ManipulationCompletedDict = new Dictionary<Predicate<string>, Action<ManipulationCompletedRoutedEventArgs>>();


        /// <summary>
        /// 需要执行的方法的关键字
        /// </summary>
        private static string methodTag;

        public static string MethodTag
        {
            get { return MetroManipulationHelper.methodTag; }
            set { MetroManipulationHelper.methodTag = value; }
        }


        /// <summary>
        /// 将容器变为多点操控容器
        /// </summary>
        /// <param name="container"></param>
        /// <param name="mode"></param>
        /// <param name="trans"></param>
        /// <param name="scale"></param>
        /// <param name="rotation"></param>
        /// <param name="containerRect">容器相对父级菜单位置</param>
        public static void InitManipulation(FrameworkElement container, ManipulationModes mode,
            double trans, double scale, double rotation, Rect containerRect)
        {

            BoundaryFeedbackDict.Add(s => s.Equals("default"), e =>
            {
                var element = e.OriginalSource as FrameworkElement;
                var con = e.Container as Panel;
                var elementBounds = element.RenderTransform.TransformBounds(new Rect(e.Position, element.RenderSize));
                Point fp = new Point((elementBounds.Left + elementBounds.Right) / 2, (elementBounds.Top + elementBounds.Bottom) / 2);
                if (fp.X < containerRect.Left ||
                    fp.X > containerRect.Right ||
                    fp.Y < containerRect.Top ||
                    fp.Y > containerRect.Bottom)
                {
                    e.Complete();
                }
            });
            container.ManipulationStarting += ElementManipulationEventHandler(container, mode);
            container.ManipulationDelta += ElementManipulationDeltaEventHandler();
            container.ManipulationStarted += ElementManipulationStartedEventHandler();
            container.ManipulationCompleted += ElementManipulationCompletedEventHandler();
            container.ManipulationInertiaStarting += ElementManipulationInertiaStartingEventHandler(trans, scale, rotation);
            foreach (var item in (container as Panel).Children)
            {
                if (item.ManipulationMode == ManipulationModes.All)
                {
                    var group = new TransformGroup();